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

android – PendingIntents继续缓存同一个对象,第1张

概述我一直面临着一些问题,试图通过意图和未决意图将数据传递给BroadcastReceiver,涉及接近警报.更具体地说,我试图传递一个对象,其中包括用户不断变化的位置.我已经尝试过在这里提出的各种策略(并且不仅仅是),但是当在BroadcastReceiver端检索intent时,导致无效值或者为首次创建的意图相同.使用的策略: >标记携带对象的意图:FLAG_ACTIVITY_NEW_TASK F 我一直面临着一些问题,试图通过意图和未决意图将数据传递给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继续缓存同一个对象所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1124125.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存