2013-12-10 37 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