Я нашел несколько тем, связанных с несколькими предупреждениями о приближении, но ни одна из них не решает мою проблему =(
У меня есть класс, который создает несколько предупреждений о близости (это важный код):
public void activateAlerts(){
Database db = new Database(activity.getApplicationContext());
Cursor cursor = db.getPois();
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
int latitudeIndex = cursor.getColumnIndex("latitude");
int longitudeIndex = cursor.getColumnIndex("longitude");
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
while(cursor.moveToNext()){
double latitude = cursor.getDouble(latitudeIndex);
double longitude = cursor.getDouble(longitudeIndex);
Bundle bundle = new Bundle();
bundle.putDouble("latitude",latitude);
bundle.putDouble("longitude",longitude);
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtras(bundle);
Random generator = new Random();
PendingIntent pi = PendingIntent.getBroadcast(activity.getApplicationContext(), generator.nextInt(), intent, 0);
getActivity().registerReceiver(new AlertIntentReceiver(), filter);
locationManager.addProximityAlert(latitude, longitude, 2500, -1, pi);
}
Log.i("ALERT", "ALERT");
}
А это приемник, который показывает уведомление:
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
double latitude = b.getDouble("latitude");
double longitude = b.getDouble("longitude");
Log.i("ALERT", latitude+","+longitude);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intentPi = new Intent(context, MainActivity.class);
PendingIntent pintent = PendingIntent.getActivity(context,0,intentPi,0);
Notification mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setContentText(context.getResources().getString(R.string.notification))
.addAction(R.drawable.ic_directions_grey_24dp,"Map",pintent)
.build();
notificationManager.notify(NOTIFICATION_ID,mBuilder);
}
Проблема в том, что я получаю слишком много BroadcastReceivers с одной и той же точкой для широты и долготы. Например, это последняя информация Logcat:
39.495412,-0.354705
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
39.4605203,-0.3478213
... [те же значения]
В чем проблема? Как я могу получить только разные широту и долготу?