进入和退出的接近警报

时间:2013-03-25 16:27:21

标签: android

我正在开发一个使用Proximity Alert检测进入和退出区域的应用程序。我只收到Entry的通知但没有退出。我使用以下代码,

  pIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 0, intent,
      PendingIntent.FLAG_CANCEL_CURRENT);

  locMgr.addProximityAlert(lat, lon, radius, 60000L, pIntent2);

  proxReceiver = new ProximityReceiver();

  IntentFilter iFilter = new IntentFilter(PROX_ALERT);
  iFilter.addDataScheme("geo");

  registerReceiver(proxReceiver, iFilter);

//广播接收器

public class ProximityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent intent) {
        if (intent.getData() != null) {
            Log.v(TAG, intent.getData().toString());
        }

        Bundle extras = intent.getExtras();
        if (extras != null) {
            final String key = LocationManager.KEY_PROXIMITY_ENTERING;
            final Boolean entering = intent.getBooleanExtra(key, false);

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

任何人都请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个

private String PROX_ALERT_INTENT = "com.example.proximityalert";
private BroadcastReceiver locationReminderReceiver;
private LocationManager locationManager;
private PendingIntent proximityIntent;

locationReminderReceiver = new ProximityIntentReceiver();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
registerReceiver(locationReminderReceiver, filter);

Intent intent = new Intent(PROX_ALERT_INTENT);

proximityIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
locationManager.addProximityAlert(lat, lon, radius, expiration, proximityIntent);