多个接近警报的问题



我发现了一些与多个接近警报相关的线程,但它们都不能解决我的问题=(

我有一个类,创建多个接近警报(这是重要的代码):

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

…(相同的值)

有什么问题吗?我怎样才能只接收到不同的经纬度?

也许你需要有一个持续的位置更新使用请求位置更新或位置监听器:

相关内容

  • 没有找到相关文章

最新更新