4

近接アラートに関して、インテントおよび保留中のインテントを介してデータを BroadcastReceiver に渡そうとするときに、いくつかの問題に直面しています。より具体的には、特にユーザーの絶えず変化する位置を保持するオブジェクトを渡そうとしています。ここで提案されているさまざまな戦術を試しましたが (それだけではありません)、どれも機能せず、BroadcastReceiver 側でインテントが取得されると、null 値または最初に作成されたインテントと同じ結果になりました。使用された戦術:

  • FLAG_ACTIVITY_NEW_TASK+FLAG_ACTIVITY_CLEAR_TOP+FLAG_ACTIVITY_SINGLE_TOP 結果:BroadacastReceiver 側の Null 値で、オブジェクトを運ぶインテントにフラグを立てます。
  • FLAG_UPDATE_CURRENT または FLAG_CANCEL_CURRENT 結果: BroadacastReceiver 側の Null 値を使用して、初期インテントを使用して作成された保留中のインテントにフラグを立てる
  • System.currentTimeMillis(); を使用して、インテントまたは保留中のインテントのランダム ID を取得します。結果: インテントが起動されない、またはまったく受信されない
  • 上記の説明はありません。結果:毎回同じ初期値を取得。

呼び出しメソッドのコード (すべての実験/null 値の生成から削除):

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 を登録する呼び出しメソッドのコード:

public void addNewCarPoint (MyCar myCar){
        IntentFilter filter = new IntentFilter(myCar.getMyCarName());
        registerReceiver(new ProximityAlertReceiver(), filter);
        setProximityAlert(myCar);
    }

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(){      
 }

}

どんなアイデアでも大歓迎です。前もって感謝します。

4

3 に答える 3

1

うーん、私は何かを見つけたと思います:

LocationListener.class が配置されている同じクラス (MyCarTracking.class) で近接アラートを検出するために使用される BroadcastReceiver (ProximityAlerReceiver) を配置しました。これにより、新しい位置情報更新への即時アクセスが提供され、新しい pendingIntent にラップされた新しいインテントが作成され、BroadcastReceiver に対して発火されます (近接基準が満たされた場合のみ)。flags: FLAG_ACTIVITY_NEW_TASK+FLAG_ACTIVITY_SINGLE_TOP と FLAG_CANCEL_CURRENT がインテントと pendingIntent でそれぞれ保持されました。すなわち:

LocationListener のコード:

private final LocationListener locationListener = new LocationListener() { 
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);//update application based on new location
        }
        public void onProviderDisabled(String provider){ 
            updateWithNewLocation(null);//update application if provider disabled
        }
        public void onProviderEnabled(String provider){
            // Update application if provider enabled
        } 
        public void onStatusChanged(String provider, int status, Bundle extras){ 
            //update application if provider hardware status changed
        }
    };

setProximityAlert() メソッドのコード:

private void setProximityAlert() { 
    String locService = Context.LOCATION_SERVICE; 
    Context context =getApplicationContext();
    LocationManager locationManager; 
    locationManager = (LocationManager)getSystemService(locService);
    float radius = myCar.getMyCarRadius(); 
    long expiration = myCar.getMyCarExpiration(); 
    Intent  intent = new Intent(CAR_DISCOVERED);
    intent.putExtra("myCar",myCar);
    locationManager.getLastKnownLocation(provider);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//flagging intent
    PendingIntent proximityIntent = PendingIntent.getBroadcast(context, -1, intent, PendingIntent.FLAG_CANCEL_CURRENT);//flagging pendingIntent         
    locationManager.addProximityAlert(myCar.getMyCarLat(), myCar.getMyCarLng(), radius, expiration, proximityIntent);//setting proximity alert
}

このソリューションは、新しい場所の更新で新しいインテントを生成します。ご協力とご関心をお寄せいただきありがとうございます:)

于 2010-12-06T12:12:03.840 に答える
0

私もこの問題に苦労してきました。私が持っていた奇妙なバグがこの問題に関連していることを見つけるのに一晩かかりました。

この件に関するGoogleコードの良い議論は次のとおりです

SetData の uri と PendingEvent.GetWhatever の (予約された) 要求コードの両方を (ab) 使用することで、すべての問題を解決しました。

また、インテントで FLAG_CANCEL_CURRENT を使用し、同じ目的を共有する保留中のインテントのみが同じデータ、アクション、URI を取得するようにしています。

少しでもお役に立てば幸いです。

于 2010-12-05T23:57:06.387 に答える
0

intent.setData(uri); を追加してみてください。ここで、uri は保留中のインテントごとに一意の値です

于 2010-12-05T16:59:08.733 に答える