4

我一直面临一些问题,试图通过意图和未决意图将数据传递到广播接收器,涉及接近警报。更具体地说,我试图传递一个对象,其中包含用户不断变化的位置。我尝试了这里提出的各种策略(不仅如此),但都没有奏效,当在 BroadcastReceiver 端检索意图时,会导致空值或与首次创建的意图相同。使用的战术:

  • 用以下方式标记携带对象的意图:FLAG_ACTIVITY_NEW_TASK+FLAG_ACTIVITY_CLEAR_TOP+FLAG_ACTIVITY_SINGLE_TOP 结果:BroadacastReceiver 端的空值
  • 标记使用初始意图创建的未决意图,带有:FLAG_UPDATE_CURRENT 或 FLAG_CANCEL_CURRENT 结果:BroadacastReceiver 端的值为空
  • 使用 System.currentTimeMillis() 获取意图或待处理意图的随机 ID;结果:根本没有触发或接收意图
  • 上面没有任何描述。结果:每次都检索相同的初始值。

调用方法的代码(从任何实验中剥离/产生空值):

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

设置意图过滤器并注册广播接收器的调用方法的代码:

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

嗯,我想我找到了一些东西:

我将用于检测接近警报的 BroadcastReceiver (ProximityAlerReceiver) 放在 LocationListener.class 所在的同一类 (MyCarTracking.class) 中。这提供了对新位置更新的即时访问,创建一个包装在新的 pendingIntent 中的新意图,以被触发到 BroadcastReceiver(仅当满足邻近条件时)。标记:FLAG_ACTIVITY_NEW_TASK+FLAG_ACTIVITY_SINGLE_TOP 和 FLAG_CANCEL_CURRENT 分别保留在意图和未决意图上。进一步来说:

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

尝试添加 intent.setData(uri); 其中 uri 是每个待处理意图的一些唯一值

于 2010-12-05T16:59:08.733 回答
0

我也一直在努力解决这个问题。我花了一整夜才发现我遇到的一个奇怪的错误与这个问题有关。

这是关于该主题的谷歌代码的一个很好的讨论:http ://groups.google.com/group/android-developers/browse_thread/thread/b2060b27c8934921

我已经通过(ab)使用 SetData 中的 uri 和 PendingEvent.GetWhatever 中的(保留的)请求代码解决了我的所有问题。

我还在我的意图上使用 FLAG_CANCEL_CURRENT 并确保只有具有相同目的的未决意图才能获得相同的数据、操作和 uri。

希望它有一点帮助。

于 2010-12-05T23:57:06.387 回答