屏幕关闭不接收广播

时间:2013-05-10 20:04:59

标签: android broadcastreceiver

屏幕关闭,广播接收器不会调用一段时间它会执行,但主要是调用wifi状态更改事件.i还设置了屏幕关闭的优先级,但没有呼叫或一些时间呼叫。你能告诉你。我的屏幕关闭我想先执行然后其他wifi状态改变将被称为

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);

        }
    }

}

1 个答案:

答案 0 :(得分:1)

您正在unregisterReceiver(wReceiver);中呼叫onPause()。这意味着每次活动进入后台时(包括屏幕关闭时),您的活动都会针对该广播取消注册。

onPause()很可能在您的Activity有机会接收广播之前被调用。

也许您想要unregisterReceiver(wReceiver);代替onDestroy()