Getting Android Sensor Events While The Screen is Off
来源:互联网 发布:苹果反删除软件下载 编辑:程序博客网 时间:2024/05/07 13:23
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
- http://mylifewithandroid.blogspot.com/2010/04/monitoring-sensors-in-background.html
- http://code.google.com/p/android/issues/detail?id=3708
- http://stackoverflow.com/a/2167311/695787
- Getting Android Sensor Events While The Screen is Off
- Android Screen on/off flow
- [转]android detect screen on and screen off
- 解决screen off 后service中的sensor不传数据问题
- Lesson 02 Getting an Image on the Screen
- 探寻Android系统中的Old CRT OFF-Screen Animation
- Gas Sensor Getting Started
- Turning Off Screen Locking
- getting a job while studying in the United States
- Creating a Game Concept: The First Step in Getting Your Game off the Ground
- What is the difference between the mouseover and mouseenter events?
- Android如何在Activity中调用Screen off 和 Screen on接口
- Android: Launch the HOME screen
- IBM WebSphere Sensor Events -GATEWAY
- android disable the Auto light off
- On-screen Rendering vs. Off-screen Rendering
- LoadRunner KB:The Web Recorder is not recording any events
- Strike while the iron is hot
- GridView用法
- 乘法计数原理
- jQuery商品列表的收缩
- jquery做复选框的全选、全部选、反选更加简单
- C#中的hello world
- Getting Android Sensor Events While The Screen is Off
- ios frame work
- 各种图形面积和周长的公式
- 不用判断语句,求两个数中最大的那个数,
- chapter 8.6: I/O事件回调函数
- 菜鸟的彷徨
- 粗谈pcap_next_ex()
- jquery选项卡的做法
- b002: 0、1背包