【发布时间】:2015-05-04 11:36:54
【问题描述】:
我在我的 Android 应用程序中实现了BroadcastReceiver(称为ProximityIntentReceiver),但我遇到了一些烦人的问题。在我的MainActivity 的onCreate() 方法中,我调用了这个函数:
addProximityAlert():
private ProximityIntentReceiver proximityIntentReceiver;
private void addProximityAlert() {
pendingIntentList = new HashMap<Integer, PendingIntent>();
double latitude = 37.567072;
double longitude = 14.273046;
Intent intent = new Intent(PROX_ALERT_INTENT);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
locationManager.addProximityAlert(
latitude, // the latitude of the central point of the alert region
longitude, // the longitude of the central point of the alert region
POINT_RADIUS, // the radius of the central point of the alert region, in meters
PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
);
pendingIntentList.put(1, proximityIntent);
proximityIntentReceiver = new ProximityIntentReceiver();
registerReceiver(proximityIntentReceiver, filter);
Toast.makeText(getApplicationContext(),"Alert Added",Toast.LENGTH_SHORT).show();
}
这是我的 MainActivity 的 onDestroy:
onDestroy():
@Override
public void onDestroy() {
super.onDestroy();
accessTokenTracker.stopTracking();
if(proximityIntentReceiver!=null) {
unregisterReceiver(proximityIntentReceiver);
removeProximityAlert();
}
}
removeProximityAlert():
private HashMap<Integer, PendingIntent> pendingIntentList;
private void removeProximityAlert () {
Iterator<Map.Entry<Integer, PendingIntent>> iterator = pendingIntentList.entrySet().iterator() ;
while(iterator.hasNext()){
Map.Entry<Integer, PendingIntent> pendingIntentMap = iterator.next();
locationManager.removeProximityAlert(pendingIntentMap.getValue());
}
}
现在,如果我在 BroadcastReceiver 或 addProximityAlert() 上更改某些内容,更改将保持不变,直到我重新启动设备。为什么?我认为通过调用unregisterReceiver() 方法足以删除BroadcastReceiver 的实例...
我应该在我的代码中添加什么?
【问题讨论】:
-
> 现在,如果我在我的 BroadcastReceiver 或我的 addProximityAlert 中更改某些内容,更改将保持不变,直到我重新启动设备。 具体来说,您要更改什么以及您期望什么去看看?
-
例如,如果我更改 POINT_RADIUS 变量或其他...
标签: android broadcastreceiver proximity ondestroy