在应用我工作,的特点之一是,当他们到达,他们已经事先设置位置通知用户。
下面的代码是在addProximityAlert在活动:
final Intent intent = new Intent(PROX_ALERT_INTENT);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(
InfoActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
locationManager.addProximityAlert(18.7726271, 98.9738381, 5000, -1,
pendingIntent);
this.locationReminderReceiver = new LocationReminderReceiver();
final IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
this.registerReceiver(this.locationReminderReceiver, filter);
@Override
protected void onPause() {
super.onPause();
if (this.locationReminderReceiver != null) {
Log.i("unregisterReceiver", "unregisterReceiver");
this.unregisterReceiver(this.locationReminderReceiver);
}
}
这里的接收器:
public class LocationReminderReceiver 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, "LocationReminderReceiver entering", Toast.LENGTH_SHORT).show();
Log.i("LocationReminderReceiver", "entering");
} else {
Toast.makeText(context, "LocationReminderReceiver exiting", Toast.LENGTH_SHORT).show();
Log.i("LocationReminderReceiver", "exiting");
}
}
}
它工作正常,但我需要调用unregisterReceiver
每一次我破坏活动-这意味着我的应用程序不再通知用户。 但我想,当他是附近的位置,直到他取消,或者已经收到通知,即使他们关闭应用程序通知用户。
我在想什么?