user2241504
user2241504

Reputation:

screen off does not Recive Broadcast

Screen Off ,Broadcast Receiver does not call some time it will execute but mostly wifi state change event is called .i have also set up the priority of screen off but does not call or some time call .can you please tell .when my screen off i want to execute first then other wifi state changed will called

BroadcastReceiver wReceiver = new ScreenReciver();

@Override
protected void onResume() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    filter.setPriority(1);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.setPriority(1000);
    registerReceiver(wReceiver, filter);
}

@Override
protected void onPause() {
    unregisterReceiver(wReceiver);
    super.onPause();

}


public class ScreenReciver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);

        switch (wifiState) {
        case WifiManager.WIFI_STATE_DISABLED:

            Intent myintent = new Intent(context, TimerClockActivity.class);
            myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myintent);
            wifiStateText = "WIFI_STATE_DISABLED";
            break;

        default:
            break;
        }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Intent myintent = new Intent(context, TimerClockActivity.class);
            myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myintent);

        }
    }

}

Upvotes: 0

Views: 372

Answers (1)

Bryan Herbst
Bryan Herbst

Reputation: 67189

You are calling unregisterReceiver(wReceiver); in onPause(). This means that every time that the Activity goes into the background (including when the screen turns off), your Activity is unregistered for that broadcast.

onPause() is likely getting called before your Activity gets a chance to receive the Broadcast.

Perhaps you want to place unregisterReceiver(wReceiver); in onDestroy() instead?

Upvotes: 1

Related Questions