GPS正在安卓服务的睡眠模式下运行

问题描述:

我已创建GPS服务,该服务定期读取位置更新。此前有一段时间GPS正在进入睡眠模式,并且之后没有位置更新。GPS正在安卓服务的睡眠模式下运行

现在我已更改我的代码以定期取消注册并注册GPS位置更新。这解决了我的GPS进入睡眠模式的问题,但创建了一个新问题。那是

我的服务在一段时间后终止。我无法检查原因。请帮忙。

这里有一些代码snipplets

@Override 
public void onCreate() { 
    super.onCreate(); 
    timer.schedule(checkLocationListener, new Date(), 1000 * 60 * 15); 
} 

private TimerTask checkLocationListener = new TimerTask() { 
    @Override 
    public void run() { 
     handler.post(new Runnable() { 
      public void run() { 
       lm.removeUpdates(LocationService.this); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, CommonConstants.GPS_POLL, 
        CommonConstants.GPS_MIN_DIST, LocationService.this); 
      } 
     }); 
    } 
}; 



@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.d(LocationService.class.getSimpleName(), "Start"); 
    valueMap.put(CommonConstants.STATUS, CommonConstants.STATUS_DROP); 
    trackMe(); 
    new Thread() { 
     public void run() { 
      while (true) { 
       try { 
        String url = DeviceConfig.getMessage(); 
        if (url == null) { 
         Thread.sleep(60000); 
         continue; 
        } 
        HttpRequester.getServerResponse(url, null); 
        DeviceConfig.addValue(CommonConstants.SERVER_UPD, "" + System.currentTimeMillis()); 
        DeviceConfig.removeMessage(); 
       } catch (Exception e) { 
        Log.e(LocationService.class.getSimpleName(), "Error", e); 
       } 
      } 
     } 
    }.start(); 
    return (START_NOT_STICKY); 
} 

private void trackMe() { 
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, CommonConstants.GPS_POLL, CommonConstants.GPS_MIN_DIST, 
     this); 
    DeviceConfig.addValue(CommonConstants.GPS_STATE, lm.isProviderEnabled(LocationManager.GPS_PROVIDER) 
     ? CommonConstants.ON : CommonConstants.OFF); 
} 
+0

嗨pramod,有没有为你工作? – viv 2013-10-08 10:50:24

+0

即使按下主页按钮时,我仍然遇到此问题......... – viv 2013-10-08 12:21:32

你不会需要一次又一次的注册/注销位置更新监听器,看来你的服务已停止,所以粘模式启动的服务,以这种方式,服务可以只能明确停止。

+0

谢谢Jitendra, 我已将其更改为非stickey。 其实我对Android开发很新,如果你能帮我开发我的应用程序,那么我会非常感激。 所以我的第一个问题是: 是否有任何理由为什么我的服务在一段时间后终止。 – Pramod 2012-02-28 11:09:43

+0

您需要将其更改为粘滞模式,而不是非粘滞模式。 – jeet 2012-02-28 11:19:00

+0

嗨,我正在尝试像这样使用它。 'Intent i = new Intent(getApplicationContext(),getClass()); PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),0,i,0); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,CommonConstants.GPS_POLL,CommonConstants.GPS_MIN_DIST,\t pi); )' 现在,如果位置发生变化,我会在哪里获取更新,以便我可以处理该问题。 – Pramod 2012-03-01 02:48:55

如果你真的想你的服务继续运行,你需要使它前景服务(见http://developer.android.com/reference/android/app/Service.html#startForeground(int,android.app.Notification))

但是,为什么不利用这个http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long,浮动,android.location.Criteria ,android.app.PendingIntent)每次更改位置时启动服务?

菲尔

+0

谢谢菲利普, 你可以请为此分享一个例子。考虑到我是Android新手,这是我的第一个应用程序。 – Pramod 2012-02-28 11:16:13

+0

仍然无法工作,GPS仍在进入睡眠模式 – Pramod 2012-03-02 06:42:41

到目前为止,我正在这样做。

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
    CommonConstants.GPS_POLL,CommonConstants.GPS_MIN_DIST, pendingIntent); 
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
    CommonConstants.NW_POLL, CommonConstants.NW_MIN_DIST, pendingIntent); 

请建议您是否有更好的解决方案。