我有一个广播接收器:
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//Do something
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Intent start=new Intent(context,MainActivity.class);
context.startActivity(start);
}
}
}
并且,在我的活动中,进入 onCreate():
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
ScreenReceiver mReceiver=new ScreenReceiver();
registerReceiver(mReceiver, filter);
问题是,当我的活动显示时,接收器正确执行操作,但是当它在后台时,有时什么也没有发生。
可能是什么问题?