当我的手机屏幕在android系统中关闭时,无法检测到震动事件 [英] unable to detect shake event when my phones screen is off in android

查看:117
本文介绍了当我的手机屏幕在android系统中关闭时,无法检测到震动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想检测到震动事件,并且我正在使用SensorEventListener,当我的活动在前台运行时,代码工作正常.但是,当我按手机的锁定按钮锁定屏幕时,无法检测到震动事件.我已经在三星gts5360上测试了我的代码.但是相同的代码在sony ericssion xperia mini pro上也可以正常工作.实际上,当我将设备闲置大约2秒钟时,我的三星手机没有检测到震动事件.锁定设备后45秒.

In my application I want to detect the shake event and I'm using SensorEventListener, the code is working fine when my activity is running in foreground. But when I press the lock button of the phone to lock the screen, the shake event can't be detected. I have tested my code on the Samsung gts5360. But the same code is working fine on sony ericssion xperia mini pro. Actually my Samsung phone is not detecting the shake events when I leave the device idle for approx. 45 seconds, after locking the device.

然后我摇动手机,它无法检测到摇动,但是当我在几秒钟的延迟后摇动手机时,它开始听摇动.我的三星手机的这种行为不一致.它会在随机的时间后开始并停止监听震动事件.

Then I shake the phone, it does not detect the shake, but when I shake phone after several seconds delay it starts listening the shake. This behavior of my samsung phone is not consistent. It starts and stop listening the shake event after a random amount of time.

现在我的问题是:这是Android功能,即屏幕锁定/关闭时设备无法检测到震动事件吗?".

Now my question is that "Is this the android feature that the device does not detects a shake event when the screen is lock/Off ?".

如果是,那么在锁定手机几秒钟后,三星手机如何开始/停止收听?

If it is, then how my samsung phone starts/stops listening after some seconds of locking the phone?

在"Sony ericssion xperia mini pro"中锁定/关闭屏幕时,它如何连续收听震动事件?

And how it is continuously listening the shake event when screen is lock/off in "Sony ericssion xperia mini pro"?.

此功能会因供应商而异吗?

Is this feature vary vendor to vendor?

如果某人需要我的代码,请告诉我,我会提供它.

If some body needs my code then let me know,I will provide it.

推荐答案

问题是,很长一段时间以来,一直没有统一的标准来确定屏幕关闭时如何处理传感器.有些设备允许它继续工作,而另一些则不能.最终,Android团队决定,要使其正常工作,应用程序应为此操作获取部分唤醒锁:

The problem is for a long time there has been no consistent standard for what to do with sensors when the screen goes off. Some devices allow it to keep working and others do not. Eventually the Android team decided that for it to work an application should acquire a partial wake lock for this kind of operation:

PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
PowerManager.WakeLock lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorRead");
lock.acquire();

需要此权限:"android.permission.WAKE_LOCK"

您需要确保在完成唤醒锁后将其释放,以便CPU可以完全进入睡眠状态.

You need to make sure you release the wake lock when you are done with it so the CPU can go fully to sleep.

即使有所有这些,它也可能无法正常工作.我发现LG手机最近不太可能支持背景传感器.同样,许多摩托罗拉手机不需要唤醒锁,而只需要在屏幕关闭时重新注册传感器即可.

Even with all of this it may not work. I've found that LG phones most recently are less likely to support background sensors. Also many Motorola phones don't require a wake lock but instead just need to re-register for the sensor when the screen goes off.

这篇关于当我的手机屏幕在android系统中关闭时,无法检测到震动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆