GPS と Bluetooth を使用するアプリを実行し、[戻る] ボタンを押して画面をオフにします。アプリの onDestroy が呼び出されたことを LogCat で確認しました。OnDestroy はロケーション リスナーを削除し、アプリの Bluetooth サービスをシャットダウンします。8時間後に電話を見ると、バッテリーの半分が消費されており、電話のバッテリー使用画面によると、私のアプリが責任を負っていました. 電話の設定メニューを使用してアプリを強制停止すると、これは発生しません。私の質問は、位置情報サービスが電力を消費するのを止めるために、リスナーを削除する以上のことをする必要がありますか? アプリがおそらく休眠しているときに、バッテリーをその程度まで消耗させることは、私が考えることができる唯一のことです.
これは、位置関連のものと Bluetooth をオンにする onStart() です。
@Override
public void onStart() {
super.onStart();
if(D_GEN) Log.d(TAG, "MainActivity onStart, adding location listeners");
// If BT is not on, request that it be enabled.
// setupBluetooth() will then be called during onActivityResult
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
// Otherwise, setup the Bluetooth session
} else {
if (mBluetoothService == null)
setupBluetooth();
}
// Define listeners that respond to location updates
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_UPDATE_INTERVAL, 0, this);
mLocationManager.addGpsStatusListener(this);
mLocationManager.addNmeaListener(this);
}
そして、これが私の onDestroy() で、それらを削除します。
public void onDestroy() {
super.onDestroy();
if(D_GEN) Log.d(TAG, "MainActivity onDestroy, removing update listeners");
// Remove the location updates
if(mLocationManager != null) {
mLocationManager.removeUpdates(this);
mLocationManager.removeGpsStatusListener(this);
mLocationManager.removeNmeaListener(this);
}
if(D_GEN) Log.d(TAG, "MainActivity onDestroy, finished removing update listeners");
if(D_GEN) Log.d(TAG, "MainActivity onDestroy, stopping Bluetooth");
stopBluetooth();
if(D_GEN) Log.d(TAG, "MainActivity onDestroy finished");
}