屏幕关闭,广播接收器在某些时间不会调用它会执行但大多数情况下会调用 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);
}
}
}