来电时如何区分屏幕开关状态?

发布于 2024-11-26 15:22:19 字数 506 浏览 1 评论 0原文

我的应用使用 TelephonyManager.ACTION_PHONE_STATE_CHANGED 执行某些操作。但我想要在用户在场(屏幕打开)时电话响起时执行不同的操作,在用户不在场(屏幕关闭)时执行不同的操作。我在 onReceive 的开头尝试了 isScreenOn() 方法(因为当屏幕关闭并且有来电时,屏幕会保持关闭一小会儿)。然而运气不佳——有时有效,有时无效。广播与屏幕状态异步...

public void onReceive(Context context, Intent intent) {
    pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    Boolean screenOn = pm.isScreenOn();
    Log.w(TAG, "Screen on is " + screenOn.toString());

我如何更改代码以真正确定电话到达时手机是否正在睡眠?

My app uses the TelephonyManager.ACTION_PHONE_STATE_CHANGED for some actions. But I want a different action while the phone rang when the user was present (screen was on) and different action when the user was not present (screen was off). I tried the isScreenOn() method just in the beginning of onReceive (because when screen is off and there is incoming call, the screen stays off for a short while). No luck however - sometimes it works, sometimes not. The broadcast is asynchronous with the screen state...

public void onReceive(Context context, Intent intent) {
    pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    Boolean screenOn = pm.isScreenOn();
    Log.w(TAG, "Screen on is " + screenOn.toString());

How could I change my code to truly determine if the phone was sleeping when the call arrived?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2024-12-03 15:22:19

好的,我得到了令我满意的答案:

public void onReceive(Context context, Intent intent) {
    KeyguardManager kg = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    Boolean screenBlocked = !kg.inKeyguardRestrictedInputMode();
    Log.w(TAG, "Screen lock is " + screenBlocked.toString());

如果屏幕被锁定,这样的解决方案会提供信息。而且,就我而言,这已经足够了 - 当来电期间屏幕被锁定时 - 这意味着没有用户。

Ok, I got the answer which satisfies me:

public void onReceive(Context context, Intent intent) {
    KeyguardManager kg = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    Boolean screenBlocked = !kg.inKeyguardRestrictedInputMode();
    Log.w(TAG, "Screen lock is " + screenBlocked.toString());

Such solution gives information if the screen is locked. And, in my case, it is enough - when screen is locked during the incoming call - it means that there was no user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文