Android Intent.ACTION_SCREEN_OFF和Intent.ACTION_USER_PRESENT如何注册

Activity has leaked IntentReceiver ScreenReceiver that was originally registered here. Are you missing a call to unregisterReceiver()?

在我的主要活动中

 // Register receiver that handles screen on and screen off logic
 final IntentFilter intentScreenfilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
 intentScreenfilter.addAction(Intent.ACTION_SCREEN_OFF);
 intentScreenfilter.addAction(Intent.ACTION_USER_PRESENT);
 screenReceiver = new ScreenReceiver();

当应用程序关闭时,我收到此消息.

ACTION_SCREEN_OFF

ACTION_SCREEN_ON 

无法在AndroidManifest中注册,只能以编程方式注册.我能做什么?我不想使用服务,因为如果服务全天运行,这对电池不利.解决办法是什么?如何使用这个接收器?

最佳答案 你不能从android清单文件注册这些接收器.它根本不支持.唯一的方法是长时间运行服务并在服务中注册这些接收器.所以,如果你真的想使用

ACTION_SCREEN_ON 

ACTION_SCREEN_OFF 

然后你必须使用服务

点赞