私はインテントと保留中のインテントを介してBroadcastReceiverに近接アラートに関するデータを渡そうとしていくつかの問題に直面していました。具体的には、オブジェクトを渡そうとしていること、ユーザーの絶えず変化する位置を保持していることなどです。私はBroadcastReceiverの側でインテントが取得されたときに、ここで提案されているさまざまな戦術を試しましたが、それだけではなく、何も働かず、null値または初回作成のインテントとなりました。使用タクティクス:PendingIntentsは同じオブジェクトをキャッシュし続けます
オブジェクトを運ぶ目的にフラグを付ける- と:FLAG_ACTIVITY_NEW_TASK + FLAG_ACTIVITY_CLEAR_TOP + FLAG_ACTIVITY_SINGLE_TOP 結果:FLAG_UPDATE_CURRENTまたはFLAG_CANCEL_CURRENT:との最初の意図を使用して作成された保留中の意図にフラグを付けるBroadacastReceiver者側のNULL値
- 、 結果:BroadcastReceiver側のヌル値
- System.currentTimeMillis()を使用して、インテントまたは保留中のインテントのランダムIDを取得しています。 結果:インテントが発射されない、または全く受信されない
- 上記の説明はありません。結果:毎回同じ初期値を取得します。 (NULL値を生成/任意的実験から剥離)呼び出しメソッドの
コード:
public void addNewCarPoint (MyCar myCar){
IntentFilter filter = new IntentFilter(myCar.getMyCarName());
registerReceiver(new ProximityAlertReceiver(), filter);
setProximityAlert(myCar);
}
:
private void setProximityAlert(MyCar myCar) {
String locService = Context.LOCATION_SERVICE;
LocationManager locationManager;
locationManager = (LocationManager)getSystemService(locService);
float radius = myCar.getMyCarRadius();
long expiration = myCar.getMyCarExpiration();
myService.setMyDriverLat(userLat);//setting user's position
myService.setMyDriverLng(userLng);//setting user's position
Intent intent = new Intent(myCar.getMyCarName());
intent.putExtra("myCar",myCar);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, -1, intent, 0);
locationManager.addProximityAlert(myCar.getMyCarLat(), myCar.getMyCarLng(), radius, expiration, proximityIntent);
}
コードインテントフィルタを設定し、BroadcastReceiverを登録、呼び出し方法
BroadcastReceiver側のコード:
public class ProximityAlertReceiver extends BroadcastReceiver {
@Override
public void onReceive (Context context, Intent intent) {
MyCar myCar=(MyCar)intent.getParcelableExtra("myCar");
driverLoc=(String)Double.toString(myCar.getMyDriverLat());
Toast.makeText(context, userLoc, Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, MyCarDiscoveryPrompt.class);
context.startActivity(i);//firing intent
}
public void intentDataLoader(){
}
}
アイデアは大歓迎です。 ありがとうございます。
私はそれも試しましたが、上記の3番目の戦術にもあるようです。/ – nifo