addProximityAlert开始工作 [英] addProximityAlert works initially
问题描述
我创造了多项ProximityAlerts的。看来只有第一部作品和某个第二。他们似乎也到期。我把这种方法来创建ProximityAlert:
I create a number of ProximityAlerts. It seems only the first works and sometime the second. They also seem to expire. I call this method to create a ProximityAlert:
public boolean addProximityAlert(int id) {
locationManager.addProximityAlert(
latitude,
longitude,
POINT_RADIUS,
PROX_ALERT_EXPIRATION,
getPendingIntent(id)
);
registerReceiver(new ProximityIntentReceiver(), getIntentFilter(id));
return true;
}
private PendingIntent getPendingIntent(int id) {
Intent intent = new Intent(PROX_ALERT_INTENT + id);
intent.setAction(String.valueOf(id));
return PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
private IntentFilter getIntentFilter(int id) {
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT + id);
filter.addAction(String.valueOf(id));
return filter;
}
在ProximityIntentReceiver扩展BroadcastReceiver的:
In ProximityIntentReceiver extends BroadcastReceiver:
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
message = context.getString(R.string.notification_alert_entering);
} else {
message = context.getString(R.string.notification_alert_exiting);
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = createNotification();
notification.setLatestEventInfo(context, context.getString(R.string.notification_alert), message, pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
}
变量:
private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATE = 1000; // in Milliseconds
private static final long POINT_RADIUS = 500; // in Meters
private static final long PROX_ALERT_EXPIRATION = -1;
private static final String PROX_ALERT_INTENT = "com.xxx.yyy.ProximityAlert";
我想创建多项独有的警报,然后进入或退出附近(可能发生多次)时,每次得到通知。所有的警报应保持有效,可有人请点我这是为什么不能正常工作的正确方向?
I'd like to create a number of unique alerts and then get notified each time when entering or exiting the proximity (can happen multiple times). All the alerts should stay valid, can someone please point me in the right direction why this is not working properly?
推荐答案
这是我怎么添加接近:
String geo = "geo:" + storeProximityData.getLatitude() + ","
+ storeProximityData.getLongitude();
Intent intent1 = new Intent(PROXIMITY_ALERT_HOME, Uri.parse(geo));
PendingIntent proximityIntent = PendingIntent.getBroadcast(
sContext.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_CANCEL_CURRENT);
sLocationManager.addProximityAlert(
storeProximityData.getLatitude(), // the
// latitude
// of
// the
// central
// point
// of
// the alert region
storeProximityData.getLongitude(), // the longitude of
// the
// central point of
// the
// alert
// region
storeProximityData.getRadius(), // the radius of the
// central
// point of the
// alert
// region, in
// meters
expirationTime, // time for this proximity alert, in
// milliseconds, or -1 to
// indicate no expiration
proximityIntent // will be used to generate an Intent to
// fire
// when entry to or exit from the alert region
// is detected
);
这就是我如何注册接收器:
And This is how I register the receiver:
private static String PROXIMITY_ALERT_HOME = "path.to.ProximityAlert";
IntentFilter filter = new IntentFilter(PROXIMITY_ALERT_HOME);
filter.addDataScheme("geo");
sContext.registerReceiver(
new ProximityResponderBroadcastReceiver(), filter);
这篇关于addProximityAlert开始工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!