私の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は停止します。