2011-03-18 75 views
1

我需要使用LocationManager中的addProximityAlert方法添加多個接近警報。問題是,當我收到意圖通知用戶接近我正在偵聽的區域之一時,如果用戶正在進入或退出讀取KEY_PROXIMITY_ENTERING的區域時收到警報,但我不知道區域是什麼他正在進入或退出。有什麼方法可以獲得這種信息?如何在接收到接近警報意圖時接收位置信息?

回答

1

以及每一個地區多個動作,添加ProximityAlert你需要的PendingIntent,一個PeindingIntent可以PendingIntent.getBroadcast獲得(aContext,aRequestCode,anIntent,0);現在,在第三個參數「anIntent」你可以把你想要的任何數據,如位置的對象,與該位置獲得一個城市的名字,

我這樣做是這樣的:現在

private static final float DEFAULT_PROXIMITY = 1000f; 
int requestCode = 0; 
for (Location p : locationList) { 
       Double _lat = p.getLatitude(); 
       Double _lon = p.getLongitude(); 
       float radious = DEFAULT_PROXIMITY; 
       long expiration = -1; 

       Intent intent = new Intent(PROXIMITY_ALERT_ACTION_FIRED); 
       intent.putExtra("location-lat" , p.getLatitude()); 
       intent.putExtra("location-lon" , p.getLongitude()); 
       PendingIntent proximityIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0); 
       locationManager.addProximityAlert(_lat, _lon, radious, expiration, proximityIntent); 
       requestCode++; 
      } 

,當你收到廣播消息,你必須從傳遞的意圖獲得位置信息,像這樣;

@Override 
public void onReceive(Context context, Intent intent) { 

    Boolean entering = intent.getBooleanExtra(key, false); 
    if(entering){ 
    long lat = intent.getLongExtra("location-lat", -1); 
    long lon = intent.getLongExtra("location-lon", -1); 
    log.info("", "entering: " + lat + lon); 
    }else{ 
    long lat = intent.getLongExtra("location-lat", -1); 
    long lon = intent.getLongExtra("location-lon", -1); 
    log.info("", "exiting: " + lat + lon); 

    } 
} 

歡呼

+0

謝謝!確切地說,我需要=)。 – Glauco 2011-03-18 18:30:12

+0

這很完美。是否可以確定觸發警報的位置提供商? GPS_PROVIDER或NETWORK_PROVIDER @Franco – 2011-12-08 07:41:27