屏幕关闭/开启Android的通知 [英] Android notification of screen off/on

查看:323
本文介绍了屏幕关闭/开启Android的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看是否有一个系统通知我可以听的,看的时候,屏幕关闭/开启。有什么想法吗?同样的事情也时网络连接/断开连接。

I'm looking to see if there's a system notification I can listen for to see when the screen turns off/on. Any thoughts? Something similar to when the network connects/disconnects.

推荐答案

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

Easiest way is to put this in your MyApplication.onCreate() method:

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的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆