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。什么也没做。有什么建议吗?我是否需要分配任何权限来捕获意图?


潇湘沐
浏览 2662回答 3
3回答

繁花如伊

我相信这些行为只能由在Java代码(via&nbsp;registerReceiver())中注册的接收者接收,而不是通过清单中注册的接收者接收。

MM们

或者,您可以使用电源管理器来检测屏幕锁定。@Overrideprotected void onPause(){&nbsp; &nbsp; super.onPause();&nbsp; &nbsp; // If the screen is off then the device has been locked&nbsp; &nbsp; PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);&nbsp; &nbsp; boolean isScreenOn;&nbsp; &nbsp; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {&nbsp; &nbsp; &nbsp; &nbsp; isScreenOn = powerManager.isInteractive();&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; isScreenOn = powerManager.isScreenOn();&nbsp; &nbsp; }&nbsp; &nbsp; if (!isScreenOn) {&nbsp; &nbsp; &nbsp; &nbsp; // The screen has been locked&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // do stuff...&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android