手机屏幕锁屏和解锁都是会发广播出来的,我们只要用BroadcaseReceiver来监听相应的Action即可,
必须动态在代码中注册才能够接受到广播。
1. public void onCreate(final Bundle savedInstanceState) {
2. final IntentFilter filter = new IntentFilter();
3. filter.addAction(Intent.ACTION_SCREEN_OFF);
4. filter.addAction(Intent.ACTION_SCREEN_ON);
5. registerReceiver(mBatInfoReceiver, filter);
6.
7.
8.
9. private final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
10. @Override
11. public void onReceive(final Context context, final Intent intent) {
12. final String action = intent.getAction();
13. if(Intent.ACTION_SCREEN_ON.equals(action)){
14. Log.d(TAG, "screen is on...");
15. }else if(Intent.ACTION.equals(action)){
16. Log.d(TAG, "screen is off...");
17.
18. }
19. }
20. };
标签:SCREEN,锁屏,final,filter,action,Intent,ACTION,Android,屏幕
From: https://blog.51cto.com/u_548275/6238196