【问题标题】:PendingIntents keep caching same objectPendingIntents 保持缓存相同的对象
【发布时间】:2010-12-01 23:08:13
【问题描述】:

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

  • 标记带有对象的意图: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; 结果:根本没有触发或接收意图
  • 上面没有描述。结果:每次都检索相同的初始值。

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

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

}

任何想法都会受到欢迎。 提前谢谢你。

【问题讨论】:

    标签: android broadcastreceiver android-intent android-pendingintent


    【解决方案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
    }
    

    此解决方案可通过新的位置更新产生新的意图。 谢谢大家的帮助和兴趣:)

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我也尝试过,但它似乎也属于上述第三种策略,没有任何意图被解雇或接收:/
      【解决方案3】:

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

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

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

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

      希望对你有所帮助。

      【讨论】:

      • 这是一个有趣的讨论,我也读过,但这里似乎有其他问题,因为我已经尝试了一切......不过谢谢你的信息:)
      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 2020-07-11
      • 2021-08-30
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多