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

But you’ll probably need a work-around forAndroid 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 aPARTIAL_WAKE_LOCK. For me this was necessary, but not sufficient. Even while holding the lock, my service ceased to receive notifications viaonSensorChanged()noronAccuracyChanged().

Re-register Your Listeners When The Screen Goes Off

Some then propose to take action when the screen turns off. Inthis 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 theBroadcastReceiverreceives anACTION_SCREEN_OFFIntent, 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 theBroadcastReceiver:

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 ofSCREEN_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 aPARTIAL_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

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android Intent 深入理解
  2. Android UI学习 - 对话框
  3. Android JNI和NDK学习(01)--搭建NDK开发
  4. Android中RelativeLayout各个属性
  5. Android UI开发第九篇――SlidingDrawer
  6. 我所理解的Android模块化(一)——模块化概
  7. 转:Android推送通知指南
  8. Android(安卓)第二季
  9. Android jni 常用方法备忘
  10. 通过JavaScript或PHP检测Android设备