您应该尝试根据显示以及上次启用的时间来处理事件。
注册一个广播接收器ACTION_SCREEN_ON,ACTION_SCREEN_OFF并ACTION_USER_PRESENT正确保存时间戳。
请注意,如果 WhatsApp 等应用程序自动启用显示屏显示新消息,则屏幕事件也可能由 WhatsApp 等应用程序触发。因此,您应该坚持使用ACTION_USER_PRESENT。
这是一些代码:
Android-Manifest.xml
<receiver android:name=".UserPresentBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
广播接收器
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)){
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:您将需要一个单独的线程(最好是守护线程)来将该时间戳与当前时间进行比较。
侦听ACTION_SCREEN_OFF启动计时器,并仅在收到时清除计时器ACTION_USER_PRESENT,这样您就不会在某些应用程序打开屏幕时意外清除计时器。
对于上述方法,您可以在启动计时器时包含以下内容。这样,您就可以在屏幕关闭时考虑自动锁定延迟。
Settings.Secure.getLong(getContentResolver(), "lock_screen_lock_after_timeout", 5000);
Run Code Online (Sandbox Code Playgroud)
或者,您可以在KeyguardManager屏幕关闭后每隔 1 毫秒左右检查一次锁定屏幕是否启用。
一般来说,这个想法是lock screen --> idle; no lock screen --> not idle
| 归档时间: |
|
| 查看次数: |
1400 次 |
| 最近记录: |