我一直面临着一些问题,试图通过意图和未决意图将数据传递给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);
}
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
}
};
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
}
该解决方案通过新的位置更新产生新的意图.
谢谢大家的帮助和兴趣:)