简体   繁体   中英

Android BroadcastReceiver is never called

I want to implement a service that handle screen on/off. To do so, I have created BootReceiver that is called at boot completed and within I start my service. However, my ScreenBroadcastReceiver is never called, why? Did I register it in the wrong place, i did it onCreate of the service... Please help me where should I register it?

So, the application should run on the background (no user interaction) and it will log the screen on and off activities. I have started these codes, but it seems that I need to register my ScreenBroadcastReceiver broadcastreceiver. If I do it onCreate of the service. should i change it ? my end goal is to run this app on background so that the application works without user interaction

This is BootReceiver that is called in the very beginning:

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.w("TAG", "BootReceiver");

        Intent service = new Intent(context, ScreenListenerService.class);
            context.startService(service);

    }

}

This is the service:

public class ScreenListenerService extends Service {


public void onCreate(){
    super.onCreate();
    Log.w("TAG", "ScreenListenerService---OnCreate ");

            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver screenOnReceiver = new ScreenBroadcastReceiver();
    registerReceiver(screenOnReceiver, filter);
}


 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {

        Log.w("TAG", "ScreenListenerService---onStart ");

         return START_STICKY;
    }

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

This is the ScreenBroadcastReceiver:

public class ScreenBroadcastReceiver extends BroadcastReceiver {

   ///this part is never called, even if the screen is turned on/off.
    public static boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

            screenOff = true;
            System.out.println("SCREEN TURNED OFF on BroadcastReceiver");
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

            screenOff = false;
            System.out.println("SCREEN TURNED ON on BroadcastReceiver");
        }
        Intent i = new Intent(context, ScreenListenerService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);

    }

}

And the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


         <service android:name=".ScreenListenerService"></service>

        <receiver android:name=".BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                 <action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

Update: Can one test this code in the emulator and let me know the result ?

Register your BroadcastReceiver in the onStart() method

@Override
public void onStart(Intent intent, int startId) {
    try {

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);

    } catch (Exception e) {
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM