2012-12-20 31 views
0

您好,我必須創建一個應用程序,用戶可以標記的位置,所以有一個報警對應的位置。我開始與BroadCast接收器我註冊廣播接收器的每一件事是工作正常,但對於一個位置Pending Intent一次又一次地觸發本文中描述的同樣的問題BroadCast接收器,待定意圖和服務混淆選擇

here

而且有很多人都面臨着同樣的問題like this one

有在第一篇文章的解決方案時,我寄存器中activty廣播接收機onCreate方法它只是工作的許多查詢時,該活動是焦點,當我按當時註冊自動取消註冊。我希望廣播接收機一直工作。其實有一個警告拿出當我點擊保存,然後接近警報保存該位置 這裏是註冊與我的應用程序

String s=System.currentTimeMillis()+""; 


Intent intent = new Intent(PROX_ALERT_INTENT+s); 
intent.putExtra("longitude", longitude); 
intent.putExtra("latitude", latitude); 

     PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), i, intent, PendingIntent.FLAG_UPDATE_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 

      1000, // the radius of the central point of the alert region, in meters 

      -1, // 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+s); 

    registerReceiver(new ProximityAlertReceiver(), filter); 

我創建與操作字符串意圖接近警報相關代碼包含一個常量和一個時間戳。然後我使用相同的常量+ time_stamp註冊一個廣播接收器。警報工作正常,但只有當活動在前臺時才起作用,否則警報不起作用。請告訴如何添加接近警報,以便它只會觸發一次或任何備用解決方案來完成此任務。

回答

0

因爲ProximityAlert發射意圖的次數更多,並且我在活動中用註釋顯示警報,所以我使用了Activity的onNewIntent方法,因此不會生成一個以上的應用程序實例。