当前位置: 动力学知识库 > 问答 > 编程问答 >

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

}

}

}

网友答案:

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?

分享给朋友:
您可能感兴趣的文章:
随机阅读: