它适用于Android 4.0.4
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null)
return;
//do something you need when broadcast received
}
};
IntentFilter filter = new IntentFilter()
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
context.registerReceiver(receiver, filter);
对于在Manifest.xml中使用动作Intent.ACTION_SCREEN_ON和Intent.ACTION_SCREEN_OFF的注册接收器,它没有帮助,因为PowerManagerService.java中的Android代码如下:
...
mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
...