位置管理器不会删除位置更新!

发布于 2024-10-17 05:12:57 字数 2230 浏览 5 评论 0原文

可能的重复:
Android:如何取消位置更新请求有意图吗?

我试图禁用我之前在不同活动中创建的待处理意图(广播),但我无法让它工作。我读过,我应该重新创建意图(具有相同的附加功能和所有内容),将其作为参数传递,以便我可以实例化挂起的意图,然后将挂起的意图作为参数传递给位置管理器的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

这不起作用,所以我认为这可能与我传入的新对象的意图有关,而不是与用于创建待处理意图的对象不同。

所以我尝试在创建挂起的意图后立即删除它,但这也不起作用:

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);

你能帮我吗??? 开始就一直困扰着...希望我有更多的声誉来限制这个...

谢谢迈克

从周三

Possible Duplicate:
Android: how to cancel a request of Location update with intent?

I am trying to disable a pending intent(broadcast) which I have previously created in a different activity but I can't get it to work. I've read that I should recreate the intent(with the same extras and everything), pass it as a parameter so that I can instantiate the pendingIntent and then pass the pendingIntent as a parameter to the location managers removeUpdates method.

In other words:


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

That didn't work so I thought that it might have to do with the intent that im passing in being a new object and not the same with the one used in order to create the pending intent.

So I tried removing the pendingIntent right after I create it but that didnt work either:

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);

Can you please help me with that??? Its been bugging be since wednesday...wish i had more reputation to put a boundy on this one...

Thanks

mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深已缘浅 2024-10-24 05:12:57

通过重新创建意图然后对待处理的意图调用 .cancel() 解决了这个问题...

Resolved it by recreating the intent and then calling .cancel() on the pending intent...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文