可能重复:
Android:如何取消有意图的位置更新请求?
我正在尝试禁用我之前在不同活动中创建的待处理意图(广播),但我无法让它工作。我读过我应该重新创建意图(具有相同的附加功能和所有内容),将其作为参数传递,以便我可以实例化 pendingIntent,然后将pendingIntent 作为参数传递给位置管理器的 removeUpdates 方法。
换句话说:
Bundle extra = new Bundle();
extra.putString("name", extras.getString("poiName")); //create same extras
extra.putInt("id", extras.getInt("rowId")); //create same extras
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra(PROX_ALERT_INTENT, extra); //put same extras in the intent
PendingIntent proximityIntent = PendingIntent.getBroadcast(this.getApplicationContext(),extras.getInt("rowId") , intent, PendingIntent.FLAG_UPDATE_CURRENT); //pass in the intent
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(proximityIntent); //remove pendingIntent
那没有用,所以我认为这可能与我作为新对象传入的意图有关,而与用于创建待处理意图的对象不同。
所以我尝试在创建它之后立即删除pendingIntent,但这也不起作用:
Bundle extras = new Bundle();
extras.putString("name", poiName);
extras.putInt("id", requestCode);
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra(PROX_ALERT_INTENT, extras);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this.getApplicationContext(), requestCode , intent, PendingIntent.FLAG_CANCEL_CURRENT);
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
);
locationManager.removeUpdates(proximityIntent);
你能帮我吗???它从星期三开始就一直在窃听……希望我有更多的声誉可以在这个问题上加个界限……
谢谢
麦克风