0

私のservice場合、一部のシナリオでは、GPSがデバイスのバッテリーを検索して消耗するのを止めることができません。

LocationListenerAgent.java

@Override
public void onCreate() {
    thisContext = this;
    // Register ourselves for location updates

    networkListener = new LocationListenerAgent();
    gpsListener = new LocationListenerAgent();

    lmgrNet = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    lmgrGps = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    gps_enabled = lmgrGps.isProviderEnabled(LocationManager.GPS_PROVIDER);
    lmgrNet.requestLocationUpdates("network", ONE_MINUTE, 10, networkListener);
    lmgrGps.requestLocationUpdates("gps", ONE_MINUTE, 10, gpsListener);

    super.onCreate();
}

@Override
public void onDestroy() {
    super.onDestroy();
    // Unregister listener
    lmgrNet.removeUpdates(this);
    lmgrNet = null;
    lmgrGps.removeUpdates(this);
    lmgrGps = null;
}

アプリを破壊するメインアクティビティで「戻る」を押すと、アプリのいくつかの場所から、これは次のように呼ばれます。

@Override
protected void onStop() {
    if (mBound) {
        unbindService(mConnection); //this is a callback to the locationlisteneragent class to bind it to a service
        mBound = false;
    }
    super.onStop();
}
@Override
protected void onDestroy() {

    if (mBound) {
        unbindService(mConnection);
        mBound = false;
    }
    stopService(new Intent(this, LocationListenerAgent.class));
    super.onDestroy();
}

多分私のsuper.Onライフサイクルメソッドはメソッドを持つコードの前にある必要があります。

GPSを停止するには他に何をする必要がありますか?他の人がこれに問題を抱えているのを見たことがありますが、答えはそれほど絶対的ではないようです。

私のアプリの他のシナリオでは、GPSは停止します。

4

3 に答える 3

2

問題を解決しました。元の投稿でmapviewについて言及していなかったことは知っていますが、MapViewも使用している場合の解決策は、mapviewが独自のGPSロケーションリスニングサービスとは関係のないGPSサービスを使用できることです。

マップビューで

private MyLocationOverlay currLocationOverlay;

@Override
protected void onStop() {
    if(currLocationOverlay!=null)
        currLocationOverlay.disableMyLocation();
    super.onStop();
}
@Override
protected void onDestroy() {
    if(currLocationOverlay!=null)
        currLocationOverlay.disableMyLocation();
    stopService(new Intent(this, LocationListenerAgent.class));
    super.onDestroy();
}
于 2012-11-21T15:53:14.727 に答える
1

あなたが投稿したコードに基づいて、私はあなたが場所の更新を削除していないと思います。

onDestroy()メソッドでは、次のようになります。

lmgrNet.removeUpdates(networkListener);
lmgrNet.removeUpdates(gpsListener);
于 2012-11-21T15:51:59.623 に答える
0

このように停止することもできます

stopService(new Intent(AlertsActivity.this、PollingService.class)); android.os.Process.killProcess(android.os.Process.myPid());

于 2013-12-19T12:38:30.950 に答える