マニフェストファイルにBroadcastReceiver
が登録されているため、アプリを終了しても場所の更新を受け取ることができます。Android:マニフェストに登録されているbroadcastreceiver内でlocationManagerを更新しないようにする
<receiver android:name="com.tenforwardconsulting.cordova.bgloc.LocationReceiver">
<intent-filter>
<action android:name="myBroadcast" />
<action android:name="stopUpdating" />
</intent-filter>
</receiver>
アプリを開くと、それはpendingIntents
を使用してlocationManager
を開始します。
Intent intent = new Intent(context, LocationReceiver.class);
intent.setAction("myBroadcast");
intent.putExtra("session_id", session_id);
//intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
lpendingIntent = PendingIntent.getBroadcast(activity.getApplicationContext(), 58534, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//Register for broadcast intents
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, lpendingIntent);
これはうまく動作し、アプリが閉鎖された後でも更新が得られます。今アップデートを停止したいときは、BroadcastReceiver
をgetComponentEnabledSetting()
で設定し、状態を無効に設定します。しかし、私は確かに私のpendingIntent
毎分を通過するだろうと確信しています。私はそれを止める方法を理解できないようです。私は...そうのような、ここに多くの回答のようにbroadcastReceiverの内側にそれを再作成
Intent intent1 = new Intent(context, LocationReceiver.class);
PendingIntent.getBroadcast(context, 58534, intent1, PendingIntent.FLAG_UPDATE_CURRENT).cancel();
を試みた。しかし、それだけで毎分これを実行した後BroadcastReceiver
に介さを続けています。私はここで何か間違っていますか?
がどのように相互に作用します:
ので、位置更新を停止するあなたのコードは次のようなものでしょうか?サービスを利用していますか?私はこのコードから多くを理解しませんでしたが、あなたが望むのは、ロケーション更新を切り替えて、サービスを作成してバックグラウンドで作業することだけです。その後、ウィジェットを使用してこのサービスを停止および開始することができます。最後に、レシーバーをサービス内に登録します。停止時に登録を解除します。 – uguboz