在android中获取解锁事件的方法?
有没有一种方法可以接收类似于在android中获取解锁事件的方法?,android,broadcastreceiver,lockscreen,Android,Broadcastreceiver,Lockscreen,有没有一种方法可以接收类似于PHONE\u UNLOCKED(使用某种类型的BroadcastReceiver)的东西 我有一个正在运行的服务,当屏幕打开时,它会显示一个Toast。不幸的是,一些手机在解锁前不会显示。大多数情况下,Toast消息已经消失。有一个广播接收器操作以下是操作的执行情况用户当前和操作关闭 将其添加到应用程序的清单中 <receiver android:name=".UserPresentBroadcastReceiver"> <intent-fil
PHONE\u UNLOCKED
(使用某种类型的BroadcastReceiver
)的东西
我有一个正在运行的服务,当屏幕打开时,它会显示一个
Toast
。不幸的是,一些手机在解锁前不会显示。大多数情况下,Toast
消息已经消失。有一个广播接收器操作以下是操作的执行情况用户当前和操作关闭
将其添加到应用程序的清单中
<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>
更新:
作为Android 8.0(API级别26)后台执行限制的一部分,针对API级别26或更高的应用程序不能再在其清单中为隐式广播注册广播接收器。
可能重复我需要关机的原因吗?我明天要试试,已经很晚了here@Chathura谢谢你的回答,但请尽量具体一点。如果不需要“意图.行动\关机”,请删除这些“死代码”。有许多“android新手”感到困惑。顺便说一句,谢谢你的回答。这就是我发表评论的原因。这将有助于某些人值得一提的是,您不会收到来自Android O的广播。请参阅:
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)) {
}
}
}