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

Location manager doesnt remove location updates!(位置管理器不会删除位置更新!)
本文介绍了位置管理器不会删除位置更新!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在尝试禁用我之前在其他活动中创建的待处理意图(广播),但我无法让它工作.我读过我应该重新创建意图(具有相同的附加功能和所有内容),将其作为参数传递,以便我可以实例化pendingIntent,然后将pendingIntent 作为参数传递给位置管理器的removeUpdates 方法.

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.

换句话说:

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.

所以我尝试在创建它后立即删除pendingIntent,但也没有用:

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...

谢谢

麦克

推荐答案

通过重新创建 Intent 然后在待处理的 Intent 上调用 .cancel() 来解决它...

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

这篇关于位置管理器不会删除位置更新!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)