Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓服务即使在Wakelocks到位的情况下也会停止_Android_Service_Wakelock - Fatal编程技术网

Android 安卓服务即使在Wakelocks到位的情况下也会停止

Android 安卓服务即使在Wakelocks到位的情况下也会停止,android,service,wakelock,Android,Service,Wakelock,我一定没有正确地实现wakelocks。我就是这样做的。这是正确的吗 private static final String LOCK_NAME_STATIC="domination.shredAssistant.locationService"; private static volatile PowerManager.WakeLock lockStatic=null; synchronized private static PowerManager.WakeLock getLock(Con

我一定没有正确地实现wakelocks。我就是这样做的。这是正确的吗

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);
}
和onStart方法

//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);
      }

}

即使我使用getLock(ctxt.acquire(),我似乎也不明白为什么在一段时间的不活动之后服务仍然被终止;谢谢你的帮助-Dom

如果一个服务已经处于活动状态很长一段时间,安卓会将其关闭。通常,开发人员会将服务设置为前置优先级,但这会导致电池寿命变差。如果您需要保持一个长期运行的服务,那么现在必须在服务类中使用新的startForeground api在通知栏中发布一个图标

我创建了一个类,如果需要的话,它可以向后兼容Android 1.5和1.6