我正在使用LocationManager#addProximityAlert
函数在输入某些地方的范围时提醒用户,如果启用了位置服务,一切正常并且我收到通知,但是当该位置被禁用时我什么也得不到。即使未启用位置,我也需要收到警报,这是我的代码:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATE, MINIMUM_DISTANCECHANGE_FOR_UPDATE,
new LocationListener());
public void prepareNotifications() {
// nearProductsLocs is an array of locations
for (int i = 0; i < nearProductsLocs.length; i++) {
Log.d(TAG, "order" + i + " " nearProductsLocs[i]);
// +++++++++++++++++++++++++++++++++++++++++++++++++
// add addProximityAlerts
Location loc = nearProducts[i];
addProximityAlert(loc.getLatitude(), loc.getLongitude());
}
}
private void addProximityAlert(Double latitude, Double longtitude, int productId) {
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra("productId", productId);
PendingIntent proximityIntent = PendingIntent.getBroadcast(
sContext.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
locationManager.addProximityAlert(latitude, longtitude, POINT_RADIUS, PROX_ALERT_EXPIRATION,proximityIntent);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
sContext.registerReceiver(new ProximityIntentReceiver(), filter);
}
我的ProximityIntentReceiver
就在这里:
public class ProximityIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent xintent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = xintent.getBooleanExtra(key, false);
int productId = xintent.getIntExtra("productId", 0);
Log.d("productId", "" + productId);
if (entering) {
Intent offerIntent = new Intent(context, Item.class);
offerIntent.putExtra("productId", productId);
PendingIntent pIntent = PendingIntent.getActivity(context, productId, offerIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification myNotification = new Notification(R.drawable.logo, "Nearby product!",System.currentTimeMillis());
createNotification(myNotification);
myNotification.setLatestEventInfo(context, "Monim", "Check this amazing Product!", pIntent);
notificationManager.notify(productId, myNotification);
}
}