gpt4 book ai didi

android - 当应用程序在android中处于 Activity 状态时获取电话锁定事件

转载 作者:行者123 更新时间:2023-11-30 01:15:52 24 4
gpt4 key购买 nike

我正在开发一个需要安全和登录系统的应用程序。我正在使用首选项来保存 session 数据。

如果任何用户退出应用程序,onPause() 方法将被调用,应用程序将清除其所有首选项数据,但这是我遇到问题的地方,当用户的屏幕被锁定时,onPause() 方法被调用并清除所有数据。如果用户屏幕被锁定,我不想清除数据。

我使用 BroadcastReceiver 来实现这个,但我想要一个优化的解决方案

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

SessionWakeBroadcastReceiver代码:

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

public class SessionWakeBroadcastReceiver extends BroadcastReceiver {

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


if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
//Some Action
}

else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
//Some Action
}
}

}

onPause() 方法中,如果电话锁定,我是否可以获取正在调用的 Intent ,然后我将不会执行数据清除。

任何帮助都会得到返回

提前致谢

最佳答案

can I get that which intent is getting called if its phone lock

您可能正在寻找 Intent.ACTION_SCREEN_OFFIntent.ACTION_SCREEN_ON 操作。

但请记住以下几点:

You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().

要检查屏幕是否被锁定,您可以这样做:

KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (km.isKeyguardLocked()) {
// locked
} else {
// not locked
}

关于android - 当应用程序在android中处于 Activity 状态时获取电话锁定事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37874050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com