問題描述
可能重復(fù):
Android:如何取消位置更新請(qǐng)求有意圖嗎?
我正在嘗試禁用我之前在其他活動(dòng)中創(chuàng)建的待處理意圖(廣播),但我無法讓它工作.我讀過我應(yīng)該重新創(chuàng)建意圖(具有相同的附加功能和所有內(nèi)容),將其作為參數(shù)傳遞,以便我可以實(shí)例化pendingIntent,然后將pendingIntent 作為參數(shù)傳遞給位置管理器的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
<小時(shí)>
這不起作用,所以我認(rèn)為這可能與我作為新對(duì)象傳入的意圖有關(guān),而與用于創(chuàng)建待處理意圖的意圖不同.
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.
所以我嘗試在創(chuàng)建它后立即刪除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);
<小時(shí)>
你能幫我嗎???從周三開始就一直在竊聽...希望我有更多的聲譽(yù)可以在這個(gè)上設(shè)置一個(gè)邊界...
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...
謝謝
麥克
推薦答案
通過重新創(chuàng)建 Intent 然后在待處理的 Intent 上調(diào)用 .cancel() 來解決它...
Resolved it by recreating the intent and then calling .cancel() on the pending intent...
這篇關(guān)于位置管理器不會(huì)刪除位置更新!的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!