4

私はインテントと保留中のインテントを介して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(){  
} 

}

アイデアは大歓迎です。 ありがとうございます。

答えて

1

うーん、私は何かを見つけまし思う:

私はLocationListener.classが配置されている同じクラス(MyCarTracking.class)、に近接アラートを検出するために使用し、BroadcastReceiver(ProximityAlerReceiver)を配置します。これにより、 は新しいロケーションの更新に即座にアクセスできるようになり、BroadcastReceiverに対して(近隣基準が満たされている場合のみ)起動される新しいpendingIntentにラップされた新しいインテントが作成されます。 フラグ:FLAG_ACTIVITY_NEW_TASK + FLAG_ACTIVITY_SINGLE_TOPおよびFLAG_CANCEL_CURRENT on intentおよびpendingIntentはそれぞれ保持されました。より具体的には:LocationListenerため

コード:setProximityAlertため

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 
     } 
    }; 

コード()メソッド:

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 
} 

この溶液を新鮮な位置更新新鮮なインテントを製造働きます。 あなたのご協力とご関心をお寄せいただきありがとうございます。

0

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

+0

私はそれも試しましたが、上記の3番目の戦術にもあるようです。/ – nifo

0

私はこの問題にも苦労しています。私が持っていた奇妙なバグがこの問題に関連していたことを知るには、一晩中かかった。

は、ここで対象にGoogleのコードには良い議論だ:http://groups.google.com/group/android-developers/browse_thread/thread/b2060b27c8934921

私はSetDataメソッドでURIとPendingEvent.GetWhatever中(予約)要求コードの両方を使用して(AB)ですべての私の問題を解決してきました。

私はFLAG_CANCEL_CURRENTを自分の意図に使用しており、同じ目的を共有している保留中のものだけが同じデータ、アクション、およびURIを取得するようにしています。

ほんの少し役に立ちます。

+0

私は興味深い議論ですveも読んでいますが、私はすべてを試して以来、ここに何か他のものが間違っているようです。 – nifo

関連する問題