我想知道當你創建一個廣播reciever您的主要活動中(在我的情況下,接近警報接收器)會發生什麼,和應用程序進程被殺掉一些未知的原因?應用程序生命週期和廣播接收機
我想在廣播recevier我註冊要收到我的接近警報,不管我的應用程序的狀態,將這種情況發生或做我需要做特別的東西,以確保?
編輯澄清:
我必須從我的應用程序中,而不是通過清單註冊的接收器。由於我想要多個proximityalerts,對於每個(不同的)位置,我都需要動態創建接收者,因爲我需要爲每個位置註冊接收者,並且使用唯一的ID很不幸。如果您希望您的BroadcastReceivers
觸發,無論你的應用程序的狀態,那麼你應該註冊他們通過你的應用程序AndroidManifest.xml
文件
double latitude = location.getLat();
double longitude = location.getLon();
Intent intent = new Intent(PROX_ALERT_INTENT_ID);
PendingIntent proximityIntent = PendingIntent.getBroadcast(activity.getApplicationContext(), 0, intent, 0);
lm.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
);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT_ID);
activity.registerReceiver(new ProximityIntentReceiver(location), filter);
你好!非常感謝您花時間回覆。我編輯了我的問題來澄清。問題是,我認爲我必須從應用程序內創建它,如果我想要聽多個接近警報,因爲在那種情況下,我需要註冊多個過濾器,並且他們將不得不動態地創建。 – Mathias
@Mathias:你必須找到一些方法來實現你的目標,讓你的接收者在清單中註冊。您無法動態註冊接收者*,並且*在您的應用未運行時讓它們可用。您可以使用單個'BroadcastReceiver'註冊多個鄰近警報,通過在每個'PendingIntent'中使用不同的額外參數進行打包,使用'getBroadcast()'的第二個參數爲'PendingIntent'提供不同的ID值,所有的事情最終都會成爲同一個對象。 – CommonsWare
@CommonsWare感謝您的時間!這是一個棘手的問題。看,如果我想擁有多個ProximityAlerts,我必須用不同的意圖ID註冊它們。但我不能預先知道這些(我不知道會有多少人),所以我不明白我可以如何將它放在清單中,因爲意圖過濾器是靜態的,我不知道預先準備過濾器ID字符串是什麼... – Mathias