0

屏幕关闭,广播接收器在某些时间不会调用它会执行但大多数情况下会调用 wifi 状态更改事件。我还设置了屏幕关闭但不调用或某些时间调用的优先级。你能告诉我什么时候屏幕关闭我想先执行,然后其他 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);

        }
    }

}
4

1 回答 1

1

你在unregisterReceiver(wReceiver);打电话onPause()。这意味着每次 Activity 进入后台(包括屏幕关闭时),您的 Activity 都会取消注册该广播。

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

也许你想unregisterReceiver(wReceiver);代替onDestroy()

于 2013-05-10T20:16:37.503 回答