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...
这篇关于位置管理器不会删除位置更新!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:位置管理器不会删除位置更新!


- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01