开发者

Android Service Stops Even with Wakelocks in Place

I must not have implemented the wakelocks correctly. This is how I did it. Is this correct?

private static final String LOCK_NAME_STATIC="domination.shredAssistant.locationService";
private static volatile PowerManager.WakeLock lockStatic=null;

synchronized private static PowerManager.WakeLock getLock(Context context) {
    if (lockStatic==null) {
        PowerManager mgr=(PowerManager)context.getSystemService(Context.POWER_SERVICE);

        lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                                    LOCK_NAME_STATIC);
   开发者_高级运维     lockStatic.setReferenceCounted(true);
    }

    return(lockStatic);
}

and the onStart method

//onStart
public void onStart( Intent intent, int startId ) {
      super.onStart( intent, startId );
      if (running == false) {
          Context ctxt=getApplicationContext();
          getLock(ctxt).acquire();
          running = true;
          readResorts();
          serviceHandler = new Handler();
          serviceHandler.postDelayed( new RunTask(),1000L );
          runtest=true;
          locupdate(0,0);
      }

}

I can't seem to figure out why the service is still killed after a period of inactivity even though I use getLock(ctxt).acquire(); Thanks for your help! -Dom


If a service has been in-active for quite sometime Android will kill it. Normally developers would set services to foregrounded priority but this caused poor battery life. If you need to keep a long running service you'll now have to use the new startForeground api in the service class to post an icon in the notification bar.

developer.android

I created a class that makes this backwards compatible to Android 1.5 and 1.6 if you need it.... ServiceForegrounder.java

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜