微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – PendingIntents继续缓存同一个对象

我一直面临着一些问题,试图通过意图和未决意图将数据传递给broadcastReceiver,涉及接近警报.更具体地说,我试图传递一个对象,其中包括用户不断变化的位置.我已经尝试过在这里提出的各种策略(并且不仅仅是),但是当在broadcastReceiver端检索intent时,导致无效值或者为首次创建的意图相同.使用的策略:

>标记携带对象的意图: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);
 }

设置intent过滤器并注册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(){      
 }

}

任何想法都会受到欢迎.
先感谢您.

解决方法:

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

我放置了broadcastReceiver(ProximityAlerReceiver),用于检测LocationListener.class所在的同一类(MyCarTracking.class)中的邻近警报.这个,
提供对新的位置更新的即时访问,创建包含在要向broadcastReceiver触发的新pendingIntent中的新意图(仅当满足接近标准时).
flags:FLAG_ACTIVITY_NEW_TASK FLAG_ACTIVITY_SINGLE_TOP和FLAG_CANCEL_CURRENT分别保存在intent和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
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐