1

マニフェストファイルに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); 

これはうまく動作し、アプリが閉鎖された後でも更新が得られます。今アップデートを停止したいときは、BroadcastReceivergetComponentEnabledSetting()で設定し、状態を無効に設定します。しかし、私は確かに私のpendingIntent毎分を通過するだろうと確信しています。私はそれを止める方法を理解できないようです。私は...そうのような、ここに多くの回答のようにbroadcastReceiverの内側にそれを再作成

Intent intent1 = new Intent(context, LocationReceiver.class); 
PendingIntent.getBroadcast(context, 58534, intent1, PendingIntent.FLAG_UPDATE_CURRENT).cancel(); 

を試みた。しかし、それだけで毎分これを実行した後BroadcastReceiverに介さを続けています。私はここで何か間違っていますか?

+0

がどのように相互に作用します:

ので、位置更新を停止するあなたのコードは次のようなものでしょうか?サービスを利用していますか?私はこのコードから多くを理解しませんでしたが、あなたが望むのは、ロケーション更新を切り替えて、サービスを作成してバックグラウンドで作業することだけです。その後、ウィジェットを使用してこのサービスを停止および開始することができます。最後に、レシーバーをサービス内に登録します。停止時に登録を解除します。 – uguboz

答えて

0

最初に設定したときと同じようにPendingIntentを正確に再作成し、位置更新コールバックを停止するためにremoveUpdates()を呼び出す必要があります。

cancel()をPendingIntentに呼び出す必要はありません。

session_idは、Applicationサブクラスのフィールドを使用するか、SharedPreferencesを使用する必要があります。これは、PendingIntentを正しく再作成するために必要です。アプリを閉じたときに

Intent intent = new Intent(context, LocationReceiver.class); 
intent.setAction("myBroadcast"); 
intent.putExtra("session_id", session_id); 
PendingIntent lpendingIntent = PendingIntent.getBroadcast(activity.getApplicationContext(), 58534, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

//Unregister for broadcast intents 
LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); 
locationManager.removeUpdates(lpendingIntent); 
+0

あなたは男です。私はそれがとても正確でなければならないことを気づかなかった。同じIDを考えた。それは今、おかげさまで動作します。 –

関連する問題