来电时如何区分屏幕开关状态?
我的应用使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我得到了令我满意的答案:
如果屏幕被锁定,这样的解决方案会提供信息。而且,就我而言,这已经足够了 - 当来电期间屏幕被锁定时 - 这意味着没有用户。
Ok, I got the answer which satisfies me:
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.