Android – 如何接收广播意图ACTION_SCREEN_ON / OFF?

<application> <receiver android:name=".MyBroadcastReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.ACTION_SCREEN_ON"></action> <action android:name="android.intent.action.ACTION_SCREEN_OFF"></action> </intent-filter> </receiver> ... </application> 

MyBroadcastReceiver设置为仅将foo吐在日志上。 什么也没做。 有什么build议吗? 我是否需要分配任何权限来捕捉意图?

我相信这些操作只能通过注册在Java代码中的接收者(通过registerReceiver() )而不是通过在清单中注册的接收者来接收。

或者,您可以使用电源pipe理器来检测屏幕locking。

 @Override protected void onPause() { super.onPause(); // If the screen is off then the device has been locked PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); boolean isScreenOn; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { isScreenOn = powerManager.isInteractive(); } else { isScreenOn = powerManager.isScreenOn(); } if (!isScreenOn) { // The screen has been locked // do stuff... } } 
 "android.intent.action.HEADSET_PLUG" "android.intent.action.ACTION_SCREEN_ON" "android.intent.action.ACTION_SCREEN_OFF" 

其中三人以上,他们不能登记使用清单。 Android核心添加“Intent.FLAG_RECEIVER_REGISTERED_ONLY”给他们(也许..我检查代码只是在“HEADSET_PLUG”的情况下。

所以我们应该使用“dynamic注册”。 像下面…

 private BroadcastReceiver mPowerKeyReceiver = null; private void registBroadcastReceiver() { final IntentFilter theFilter = new IntentFilter(); /** System Defined Broadcast */ theFilter.addAction(Intent.ACTION_SCREEN_ON); theFilter.addAction(Intent.ACTION_SCREEN_OFF); mPowerKeyReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String strAction = intent.getAction(); if (strAction.equals(Intent.ACTION_SCREEN_OFF) || strAction.equals(Intent.ACTION_SCREEN_ON)) { // > Your playground~! } } }; getApplicationContext().registerReceiver(mPowerKeyReceiver, theFilter); } private void unregisterReceiver() { int apiLevel = Build.VERSION.SDK_INT; if (apiLevel >= 7) { try { getApplicationContext().unregisterReceiver(mPowerKeyReceiver); } catch (IllegalArgumentException e) { mPowerKeyReceiver = null; } } else { getApplicationContext().unregisterReceiver(mPowerKeyReceiver); mPowerKeyReceiver = null; } } 

我实现这个的方式是通过在onCreate()的主要活动中注册接收者,事先在某处定义接收者:

  lockScreenReceiver = new LockScreenReceiver(); IntentFilter lockFilter = new IntentFilter(); lockFilter.addAction(Intent.ACTION_SCREEN_ON); lockFilter.addAction(Intent.ACTION_SCREEN_OFF); lockFilter.addAction(Intent.ACTION_USER_PRESENT); registerReceiver(lockScreenReceiver, lockFilter); 

然后onDestroy():

  unregisterReceiver(lockScreenReceiver); 

在接收器中,您必须捕获以下情况:

 public class LockScreenReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent != null && intent.getAction() != null) { if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { // Screen is on but not unlocked (if any locking mechanism present) } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { // Screen is locked } else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) { // Screen is unlocked } } } }