2013-12-10 85 views
0

我有一个Android的位置侦听器的问题。 由于某种原因,它会停下来并被杀死。Android位置服务被杀

让我们从一开始就着手。 我有一个活动将启动GPS。

private void CheckEnableGPS() { 
    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    //  String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
    if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     startGPS(); 
    } else { 
     buildAlertMessageNoGps(); 
    } 

} 

private void startGPS() 
{ 
    Toast.makeText(GPSActivity.this, "GPS Enabled", Toast.LENGTH_LONG).show(); 
    getApp().startGPSMonitor(); 
} 

首先,我将GPSService绑定到我的应用程序。

bindService(new Intent(this, GpsService.class), mGpsServiceConnection, Context.BIND_AUTO_CREATE); 

现在我只启动一个服务,而不是结合它

startService(new Intent(this, GpsService.class)); 

的GPSService类:

@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.d(MobileConnectorApplication.APPLICATION_TAG, "Started GPS Service"); 
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, THIRTY_SECONDS, 0, this); 
    } else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, THIRTY_SECONDS, 0, this); 
    } 
} 

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return START_STICKY; 
    } 

@Override 
public void onLocationChanged(Location location) { 
    MobileConnectorApplication app = ((MobileConnectorApplication) getApplication()); 
    Location oldLocation = app.getLastGpsLocation(); 

    if (isBetterLocation(location, oldLocation)) { 
     updateLocation.latitude = (float) location.getLatitude(); 
     updateLocation.longitude = (float) location.getLongitude(); 
     updateLocation.user = app.getLoggedInUser(); 

     try { 
      updateLocation.execute(); 
      app.setLastGpsLocation(location); 
     } catch (IOException e) { 
      Log.e(MobileConnectorApplication.APPLICATION_TAG, "GPSSERVICE - Sending action failed", e); 
     } catch (HttpException e) { 
      Log.d(MobileConnectorApplication.APPLICATION_TAG, "GPSSERVICE - Httpexception", e); 
     } 
     notifyBroadCastListeners(location); 
    } 
} 

我需要这全天候运行,以每30秒更新。不要谈论电池消耗等等,因为这对我来说不是问题。我试过在这个服务的oncreate事件中声明一个创建的wakelock。 它似乎没有帮助。 我确实读过这个位置侦听器应该有它自己的唤醒锁?这是真的? 我需要在其他地方声明一个唤醒锁吗?

有没有人可以帮助我做到这一点?

欲了解更多信息,我得到了一些日志,其中locationprovider被杀害。 另外,我的应用程序是否被回收? (我需要重新登录)

13:24:24.835: D/ConnectivityService(1475): ConnectivityService FeatureUser expire(0, enableSUPL, [email protected]) 
13:24:24.835: D/ConnectivityService(1475): stopUsingNetworkFeature for net 0: enableSUPL 
13:24:24.835: D/ConnectivityService(1475): ignoring - this process has no outstanding requests 
13:25:47.453: D/TrackDroid(2341): Executing [email protected]0591dc0 
13:25:54.195: D/SntpClient(1475): request time failed: java.net.SocketTimeoutException: Try again 
13:25:54.195: D/GpsLocationProvider(1475): requestTime failed 
13:27:55.523: D/CallManager(1539): handleMessage (EVENT_SERVICE_STATE_CHANGED) 
13:27:55.796: D/StatusChecker(5014): onReceive : android.intent.action.SERVICE_STATE 
13:27:55.804: D/StatusChecker(5014): Service state changed : 0 
13:27:55.812: D/StatusChecker(5014): trySendSMS, in service : true , SMS send : false , SMSC : true 
13:27:55.828: D/MobileDataStateTracker(1475): replacing old mInterfaceName (rmnet0) with rmnet0 for hipri 
13:27:55.851: D/MobileDataStateTracker(1475): supl Received state= CONNECTED, old= CONNECTED, reason= (unspecified), apnTypeList= default,supl 
13:27:55.921: D/MobileDataStateTracker(1475): default Received state= CONNECTED, old= CONNECTED, reason= (unspecified), apnTypeList= default,supl 
13:27:55.937: W/GpsLocationProvider(1475): Unneeded remove listener for uid 1000 
13:27:55.937: D/GpsLocationProvider(1475): stopNavigating 
13:27:55.937: V/GpsLocationProvider(1475): reportStatus status: 2 
13:27:55.937: D/GpsLocationProvider(1475): send an intent to notify that the GPS has been enabled or disabled 
13:27:55.960: V/GpsLocationProvider(1475): reportStatus status: 2 
13:27:55.984: D/ProgramMonitor(4740): onReceive -no 
13:27:56.015: V/GpsLocationProvider(1475): reportStatus status: 2 
13:27:56.085: I/Launcher(1549): onResume() ended 
13:27:56.132: I/Launcher(1549): onPause() 
13:27:56.414: D/dalvikvm(1475): GC_CONCURRENT freed 1616K, 40% free 6385K/10503K, external 3094K/3852K, paused 12ms+27ms 

回答

0

您应该阅读有关service的生命周期。当系统需要更多资源时,他们可能会关闭。

有以下几种解决方案:您可以使用startForgraound()来防止系统回收它。或者你可以听系统事件来开始你的服务。例如,当用户打开设备,当用户将设备充电或其他任何东西时。

+0

startforeground似乎已经做到了。谢谢! – Robin