Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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,PendingIntent.FLAG_CANCEL_CURRENT);//flagging pendingIntent         
    LOCATIOnManager.addProximityAlert(myCar.getMyCarLat(),proximityIntent);//setTing proximity alert
}

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

大佬总结

以上是大佬教程为你收集整理的android – PendingIntents继续缓存同一个对象全部内容,希望文章能够帮你解决android – PendingIntents继续缓存同一个对象所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。