【发布时间】:2011-02-11 07:14:38
【问题描述】:
可能重复:
Android: how to cancel a request of Location update with intent?
我正在尝试禁用我之前在其他活动中创建的待处理意图(广播),但我无法让它工作。我读过我应该重新创建意图(具有相同的附加功能和所有内容),将其作为参数传递,以便我可以实例化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);
你能帮我吗???从周三开始就一直在窃听...希望我有更多的声誉可以在这个上设置一个边界...
谢谢
麦克
【问题讨论】:
-
Mike,如果你创建了一个 PendingIntent 然后在它上面 requestLocationUpdates(),你必须在那个 same PendingIntent 对象上删除Updates()。您不能使用 new PendingIntent() 或 Intent .. 您必须以某种方式将原始 PendingIntent 用于第二个活动。
-
@DJC 我设法通过重新创建和取消挂起的意图来使其工作......显然,只要你传递具有完全相同的附加功能的意图,它是否不是实际对象并不重要您可以获取您之前创建的待处理意图...有点奇怪但它有效...无论如何感谢您的回复...
-
@DJC 好吧,现在我想起来了...您基本上是在覆盖以前的 pendingIntent 对象,因此它不再存在,而新的对象取而代之...所以您有一个实际的参考待处理的意图,然后您可以取消它:)
-
CommonsWare 在这里给出了非常明确的答案:Android: how to cancel a request of Location update with intent?
标签: android geolocation android-intent locationmanager android-pendingintent