2013-03-03 62 views

回答

6

的KEY_PROXIMITY_ENTERING是通常用於確定設備是否進入或退出。

你應該先註冊到的LocationManager

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
Intent intent = new Intent(Constants.ACTION_PROXIMITY_ALERT); 
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0); 

locationManager.addProximityAlert(location.getLatitude(), 
    location.getLongitude(), location.getRadius(), -1, pendingIntent); 

的的PendingIntent將會被用來生成一個Intent檢測到從警報區域進入或退出時觸發。 你應該定義廣播接收器接收從發送的LocationManager廣播:

public class YourReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     final String key = LocationManager.KEY_PROXIMITY_ENTERING; 
     final Boolean entering = intent.getBooleanExtra(key, false); 

     if (entering) { 
      Toast.makeText(context, "entering", Toast.LENGTH_SHORT).show(); 
     } else { 
      Toast.makeText(context, "exiting", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

然後在你的清單註冊的接收器。

<receiver android:name="yourpackage.YourReceiver " > 
    <intent-filter> 
     <action android:name="ACTION_PROXIMITY_ALERT" /> 
    </intent-filter> 
</receiver> 
+0

另一個問題 - 我的接收器已經通過時間**廣播處理**提醒。我怎樣才能定義哪個提醒觸發了這個動作?他們兩個都是從同一個班上被解僱的 - @Jermaine Xu – Daniel 2013-03-04 07:41:02

+0

順便說一句,關於主要問題的答案。似乎這是答案 - @Jermaine Xu – Daniel 2013-03-04 07:44:10

+0

我想你想要intent.getAction(),不是嗎? – StarPinkER 2013-03-04 07:50:06

相關問題