如何在同时来电/关状态屏幕分辨? [英] How to distinguish the screen on/off status while incoming call?
问题描述
我的应用程序使用 TelephonyManager.ACTION_PHONE_STATE_CHANGED
的一些行动。但我想不同的操作,而手机响了起来,当用户是present(屏幕是打开的)和不同的操作,当用户没有present(屏幕是关闭)。我试过 isScreenOn()
方法只在的onReceive
的开始(因为当屏幕关闭,并有来电,屏幕一直不亮一小会儿)。然而,没有运气 - 有时它的工作原理,有时没有。广播是异步的屏幕状态...
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());
我
如何改变我的code真正确定,如果手机在睡觉时,电话来了吗?
How could I change my code to truly determine if the phone was sleeping when the call arrived?
推荐答案
好吧,我得到了满足我的答案:
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.
这篇关于如何在同时来电/关状态屏幕分辨?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!