我在我的Android應用程序中實現了一個BroadcastReceiver
(稱爲ProximityIntentReceiver
),但我有一些煩人的問題。在我MainActivity
的onCreate()
方法我調用這個函數:Android - 如何銷燬BroadcastReceiver?
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中的某些內容,那麼更改將保持不變,直到我重新啓動設備._具體而言,您在更改什麼以及您期望看到什麼? – user3452758
例如,如果我更改POINT_RADIUS變量或其他東西... –