2009-10-19 61 views
60
<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吐出日志。什么也没做。 有什么建议吗?我是否需要分配任何权限来捕捉意图?Android - 如何接收广播意图ACTION_SCREEN_ON/OFF?

回答

66

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

+1

OK,我只是想通。这背后的理由是什么? – ohnoes 2009-10-19 12:06:06

+17

对于那些他们真的不想启动一个新流程的情况,Android似乎不支持清单注册的接收器。例如,您将看到与电池信息操作(例如,BATTERY_LOW)相同的效果。但是,除此之外,我没有太多理由 - 我没有写出来。 :-) – CommonsWare 2009-10-19 12:50:33

+1

@CommonsWare那么我怎样才能registerReceiver()当按下电源按钮? – Venky 2012-06-21 06:16:53

27

或者,您可以使用电源管理器来检测屏幕锁定。

@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... 
    } 
} 
+0

+ PowerManager提供的方法+1如果有人不想使用'BroadcastReceiver',那么非常好的接触 – Sajmon 2013-08-06 11:20:07

+3

只是想补充一点,虽然这对于99%的情况非常适用,但在特定情况下,这实际上可能会失败。在可以快速打开和关闭屏幕的设备(如Galaxy S4)中,可以通过将其与proximityLock组合来检查此行为失败。如果您触发锁定快速关闭屏幕,isScreenOn实际上会在onPause()中返回true。 – 2014-01-24 08:25:50

+0

+1完美答案给我 – 2014-07-11 05:42:22

26
"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”情况。

所以,我们应该用“动态注册”。 像下面...

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; 
    } 
} 
+1

非常感谢,找到了我正在寻找的东西 – 2013-06-23 06:22:26

+0

@AkhilJain没问题! :) – cmcromance 2013-06-25 00:26:30

+0

+1,很好。唯一需要注意的是Build.VERSION.SDK不被弃用。 – 2013-08-12 10:14:08

3

我实现了这是在我的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 
      } 
     } 
    } 
} 
+0

**感谢**这个清晰的代码人 - ** @ box **! +1 – 2018-03-09 13:53:42