一种在android中获取解锁事件的方法?

Posted

技术标签:

【中文标题】一种在android中获取解锁事件的方法?【英文标题】:A way to get unlock event in android? 【发布时间】:2013-12-12 01:00:57 【问题描述】:

有没有办法接收PHONE_UNLOCKED 之类的东西(带有某种BroadcastReceiver)?

我有一个正在运行的服务,当屏幕打开时显示Toast。不幸的是,一些手机在解锁之前不会显示它。大多数时候Toast 消息已经消失了。

【问题讨论】:

android - detect phone unlock event, not screen on的可能重复 【参考方案1】:

有一个Broadcast Receiver ActionACTION_USER_PRESENT这里是ACTION_USER_PRESENT和ACTION_SHUTDOWN的实现

将此添加到您的应用程序清单

<receiver android:name=".UserPresentBroadcastReceiver">
  <intent-filter>
    <action android:name="android.intent.action.USER_PRESENT" />
    <action android:name="android.intent.action.ACTION_SHUTDOWN" />
 </intent-filter>
</receiver>

接收动作

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class UserPresentBroadcastReceiver extends BroadcastReceiver 

    @Override
    public void onReceive(Context arg0, Intent intent) 

        /*Sent when the user is present after 
         * device wakes up (e.g when the keyguard is gone)
         * */
        if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))

        
        /*Device is shutting down. This is broadcast when the device 
         * is being shut down (completely turned off, not sleeping)
         * */
        else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) 

        
    


更新:

作为 Android 8.0(API 级别 26)后台执行限制的一部分,以 API 级别 26 或更高级别为目标的应用不能再在其清单中为隐式广播注册广播接收器。 see

【讨论】:

我需要 ACTION_SHUTDOWN 做什么? 我明天去试试,这里已经是深夜了 @Chathura 感谢您的回答,但请尝试具体。如果不需要“Intent.ACTION_SHUTDOWN”,请删除那种“死代码”。有许多“机器人新生”感到困惑。顺便说一句,谢谢你的回答。 这就是我发表评论的原因。这将对某人有所帮助 值得一提的是你不会收到来自Android O的广播。见:developer.android.com/guide/components/…

以上是关于一种在android中获取解锁事件的方法?的主要内容,如果未能解决你的问题,请参考以下文章

Android GeoLocation FireStore:一种在用户关闭位置共享选项后阻止谷歌服务获取用户地理点的方法

在Windows XP上系统锁定/解锁时触发事件

如何提供一种在 Android 库中使用自定义加密的方法?

一种在 NodeJS 中在 Instagram 中获取和发送直接消息的方法

一种在多个android测试模块之间共享代码的方法

如何在 Android 中使用长按事件