Android位置服务被杀死

时间:2013-12-10 10:40:11

标签: android android-location

我在android中遇到了locationlistener的问题。 出于某种原因,它会停止并被杀死。

让我们从头开始吧。 我有一个将启动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事件中创建的唤醒锁。 它似乎没有帮助。 我确实读过locationlistener应该拥有它自己的唤醒锁?这是真的? 我是否需要在其他地方宣布唤醒锁?

有人可以帮我实现这个目标吗?

有关更多信息,我得到了一些日志,其中locationprovider被杀死。 此外,我的应用程序是否被回收? (之后我需要重新登录)

13:24:24.835: D/ConnectivityService(1475): ConnectivityService FeatureUser expire(0, enableSUPL, android.os.Binder@407cf348)
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 be.resultants.trackdroid.services.actions.UpdateLocationAction@40591dc0
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

1 个答案:

答案 0 :(得分:0)

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

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

相关问题