屏幕关闭/打开的Android通知

标签 android

我正在查看是否有我可以收听的系统通知,以查看屏幕何时关闭/打开。有什么想法吗?类似于网络连接/断开连接时的情况。

最佳答案

最简单的方法是把它放在你的 MyApplication.onCreate() 方法中:

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.d(TAG, Intent.ACTION_SCREEN_OFF);
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.d(TAG, Intent.ACTION_SCREEN_ON);
        }
    }
}, intentFilter);

关于屏幕关闭/打开的Android通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4208458/

相关文章:

android - 无法在 onCreate 语句中获取对微调器的引用

Android - 如果 View 不可见,则在 RecyclerViewAdapter 中添加 MapFragment 失败

android - Handler.postdelayed()在滚动状态更改时无法在recyclerview内部工作

android - Camera2 Api android 上的 ImageReader

java - 从 JobIntentService 调用 Activity 方法

android - 如何将我的数据库的 AsyncTask 与 android 中的谷歌地图结合起来?

android - 如何在Android中查找蓝牙配对失败的原因?

Android CountDownTimer 减慢应用程序页面转换

java - Android 设置具有唯一 ID 的闹钟

Android 应用程序错误 - 您必须在 Google Cloud 项目上启用计费