多个接近警报的问题

时间:2015-07-27 14:32:56

标签: android broadcastreceiver alert android-pendingintent proximity

我发现了一些与多个邻近警报相关的线程,但没有一个解决了我的问题=(

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

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

     

... [相同的价值]

有什么问题?我怎样才能获得不同的纬度和经度?

1 个答案:

答案 0 :(得分:0)

您可能需要使用请求位置更新或位置监听器继续更新位置:

相关问题