gpt4 book ai didi

android - PendingIntents 保持缓存相同的对象

转载 作者:太空宇宙 更新时间:2023-11-03 10:56:07 26 4
gpt4 key购买 nike

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

  • 标记携带对象的 Intent :FLAG_ACTIVITY_NEW_TASK+FLAG_ACTIVITY_CLEAR_TOP+FLAG_ACTIVITY_SINGLE_TOP结果:BroadacastReceiver 端的空值
  • 标记使用初始 Intent 创建的待定 Intent ,带有:FLAG_UPDATE_CURRENT 或 FLAG_CANCEL_CURRENT结果:BroadacastReceiver 端的空值
  • 使用 System.currentTimeMillis() 获取 Intent 或未决 Intent 的随机 ID;结果:根本没有触发或接收 Intent
  • 上面没有描述。结果:每次取回相同的初始值。

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

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) 中。这个,提供对新位置更新的即时访问,创建一个包裹在新 pendingIntent 中的新 Intent ,以触发 BroadcastReceiver(仅当满足邻近条件时)。flags:FLAG_ACTIVITY_NEW_TASK+FLAG_ACTIVITY_SINGLE_TOP 和 FLAG_CANCEL_CURRENT on 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
}

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

关于android - PendingIntents 保持缓存相同的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4330071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com