2010-10-05 68 views
1

這是我的第一個問題:Android的接近警戒不被解僱

我試圖接近配置警報將從數據庫和互聯網服務提供商食。但我在配置簡單的近距離警報進行測試時遇到問題。我設法創建警報,但它永遠不會被解僱,我現在只在模擬器中嘗試,並且不知道是否需要一些額外的代碼來觸發警報。

我讀過的地方,GPS供應商禁用,使網絡供應商可以用來觸發模擬器上的警報。

我的代碼如下所示:

接近意向聲明

private String proximityIntentAction = new String("gpsdelivery.gpship.getLocation.GPS_PROXIMITY_ALERT");  

內部在onStart(),其中的警報參數設置

IntentFilter intentFilter = new IntentFilter(proximityIntentAction); 
registerReceiver(new ProximityAlert(), intentFilter); 
setProximityAlert(45.150344, 9.999815, -1);   

接近警報功能在警報得到已創建

private void setProximityAlert(double lat, double lon, int requestCode) 
{ 
    // 100 meter radius 
    float radius = 100f; 
    // Expiration is 10 Minutes 
    long expiration = 600000; 
    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    Intent intent = new Intent(proximityIntentAction); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    locManager.addProximityAlert(lat, lon, radius, expiration, pendingIntent); 

} 

最後我接近與廣播接收機警報類

public class ProximityAlert extends BroadcastReceiver 
    { 
     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      Log.v("SomeTag","Proximity alert received"); 

     } 

    }  

請讓我知道我缺少什麼或者我做錯了。在此先感謝,任何源代碼將不勝感激。

+0

爲什麼你認爲你應該廣播接近警報?你在DDMS的模擬器中設置了一個位置嗎? – Falmarri 2010-10-05 23:37:23

+0

我認爲廣播接收機需要它來執行接近警報被觸發後的動作,至少這是我在其他示例中看到的。我確實在DDMS中的鄰近範圍內設置了多個位置,但警報永遠不會被觸發,我得到了這個錯誤信息:10-07 01:22:50.681:ERROR/ActivityThread(796):android.app.IntentReceiverLeaked:Activity gpsdel​​ivery.gpship .getLocation泄漏了最初在此處註冊的IntentReceiver gpsdel​​[email protected]。你是否錯過了對unregisterReceiver()的調用? – flavs 2010-10-07 05:58:06

回答

0

嗯,我相信你的setProximityAlert(45.150344, 9.999815, -1);是不正確的。具體來說,你的-1。它不包含locManager.addProximityAlert(lat, lon, radius, expiration, pendingIntent);中所需的信息。