[置顶] Android问题-----Getting Android Sensor Events While The Screen is Off

本文介绍了解决Android系统中传感器事件在屏幕关闭后无法持续接收的问题。通过持有PARTIAL_WAKE_LOCK并重新注册监听器的方法,确保了即使在屏幕关闭的情况下也能继续接收到传感器事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

So you want to continue to get sensor events in your Android service, even after the screen turns off? Well that’s too damn bad, kid, cause you can’t. No, I’m just kidding.

But you’ll probably need a work-around for Android Issue 3708. In this document I describe what I had to do to get it working on my platform. My assumption is you’ve already been Googling around, and so have a little background on this, but are still scratching your head as to why it’s not working. Hopefully, you won’t be in that position after reading this!

Hold a PARTIAL_WAKE_LOCK

Some relic platforms of the past might have skated by holding just a PARTIAL_WAKE_LOCK. For me this was necessary, but not sufficient. Even while holding the lock, my service ceased to receive notifications via onSensorChanged() nor onAccuracyChanged().

Re-register Your Listeners When The Screen Goes Off

Some then propose to take action when the screen turns off. In this post on StackOverflow, for example, the author proposes un-registering and re-registering to receive sensor events at that time.

Use a Delayed Thread, for the Above

The above two techniques are ultimately what I’ve used to solve the problem, except I had to make one additional modification. When the BroadcastReceiver receives anACTION_SCREEN_OFF Intent, I queue a thread to run 500ms in the future, instead of running immediately. This ensures that most other activities surrounding the screen off will have completed. So at that time, 500ms later, we un-register and re-register our service as a sensor event listener.

This is how I’ve implemented the BroadcastReceiver:

public BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "onReceive("+intent+")");
 
        if (!intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
            return;
        }
 
        Runnable runnable = new Runnable() {
            public void run() {
                Log.i(TAG, "Runnable executing.");
                unregisterListener();
                registerListener();
            }
        };
 
        new Handler().postDelayed(runnable, SCREEN_OFF_RECEIVER_DELAY);
    }
};

You may have to play with the value of SCREEN_OFF_RECEIVER_DELAY — for me, 500 (milliseconds) was the minimal value that would get it working.

See the Code In An Example App

I’ve posted an example app which includes the above code. It shows a foreground service holding a PARTIAL_WAKE_LOCK, doing the re-registration business mentioned above. The result is a service which continues to receive sensor events even after the screen goes off (for whatever reason.)

https://github.com/jamesonwilliams/AndroidPersistentSensors

Well, it worked for me at least. Does it work for you? (Please comment below.)

References


转载于:https://www.cnblogs.com/anjon520/archive/2013/04/01/3249935.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值