0

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

  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();
            }
        }
    }
}

任何人请帮助。提前致谢。

4

1 回答 1

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);
于 2013-05-09T19:38:02.617 回答