Proximity Alert firing twice?

2019-08-01 02:45发布

问题:

Receiver:

public class ProximityAlert extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        int id = intent.getIntExtra("id", -1);
        String title = intent.getStringExtra("Title");

        Intent showDialog = new Intent(context, ShowMapDialog.class);
        showDialog.putExtra("id", id);
        showDialog.putExtra("Title", title);
        context.startActivity(showDialog);
    }

}

ShowMapDialog.java:

public class ShowMapDialog extends Activity {

PowerManager.WakeLock wakeLock;
AlertDialog alertbox;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();

    Prefs sp = new Prefs();

    int lastplaceid = sp.getLastPlaceID(getApplicationContext());
    boolean nopopup = sp.getNoPopup(getApplicationContext());

    final int id = extras.getInt("id");
    String Title = extras.getString("Title");

    Log.d("id+title",id+"+"+Title);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Place of Interest");

    if(id != lastplaceid && !nopopup) {
        wakeLock.acquire();

        sp.setLastPlaceID(getApplicationContext(), id);

        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        int dot = 200;
        int dash = 500;
        int long_gap = 1000;
        long[] pattern = {0, dash, dot, dash, long_gap};
        v.vibrate(pattern, -1);

        alertbox = new AlertDialog.Builder(ShowMapDialog.this).create();
        alertbox.setTitle(getString(R.string.dialogTitle));
        alertbox.setMessage(getString(R.string.dialogShowPlaceText1)+Title+getString(R.string.dialogShowPlaceText2));
        alertbox.setButton(getString(R.string.dialogShowPlaceYes), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent showPlace = new Intent(getApplicationContext(),Showplace.class);
                showPlace.putExtra("id", id);
                startActivity(showPlace);
            }
        });
        alertbox.setButton2(getString(R.string.dialogShowPlaceNo), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        alertbox.show();
    } else if(id != lastplaceid && nopopup){
        wakeLock.acquire();
        sp.setLastPlaceID(getApplicationContext(), id);
        Intent showPlace = new Intent(getApplicationContext(),Showplace.class);
        showPlace.putExtra("id", id);
        startActivity(showPlace);
    } else {
        finish();
    }
}

@Override
public void onPause(){
    super.onPause();
    wakeLock.release();
    alertbox.dismiss();
    finish();
}

}

Creating ProximityAlerts:

    private void setProximityAlert(String Title, double lat, double lon, float radius, final int id, int requestCode){
        // Expiration is x Minutes (x mins * 60secs * 1000milliSecs)
        long expiration = -1;

        Intent intent = new Intent(PROXIMITY_INTENT_ACTION);
        intent.putExtra("id", id);
        intent.putExtra("Title", Title);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        mlocManager.addProximityAlert(lat, lon, radius, expiration, pendingIntent);
    }

    public void placeMarkersPoints(){
        this.dh = new DataHelper(ShowMap.this);
        List<Pontos> list = this.dh.selectAll();
        markerPlaces = new OverlayPlaces(getResources().getDrawable(R.drawable.marker_places), mapView);
        for(Pontos p : list){
             markerPlaces.addPoint(new GeoPoint(p.getLat(),p.getLng()),p.getName().toString(),Integer.toString(p.getId()));
             setProximityAlert(p.getName().toString(), p.getLat(), p.getLng(), p.getRadius(), p.getId(), p.getId());
        }
        mapView.getOverlays().add(markerPlaces);
        mapView.invalidate();
    }

I register the receiver on the onCreate like this:

    br = new ProximityAlert();
    mIntentFilter = new IntentFilter(PROXIMITY_INTENT_ACTION);

onPause:

@Override
public void onPause() {
    super.onPause();
    mlocManager.removeUpdates(this);
    unregisterReceiver(br);
}

onResume:

@Override
protected void onResume() {
    super.onResume();
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this);
    registerReceiver(br, mIntentFilter);
}

When I go through a second ProximityAlert, it does this code twice: Log.d("id+title",id+"+"+Title);

Any ideas? If needed, I can post more code :)

回答1:

Actually my problem was with the BroadcastReceivers.

I was destroying them but not quite correctly, so sometimes they would double, or triple, or w/e.

Thanks but I solved it myself :)



回答2:

You are registering the receiver every time the application resumes.

@Override
protected void onResume() {
    super.onResume();
    ...
    registerReceiver(br, mIntentFilter); 
}

Consequently, every time each proximity alert is triggered (PendingIntent) multiple instances of the receiver will be fired (one for every time the activity is resumed). Imagine that you have one caller and a universe of cloned receivers, who ALL answer back at the same time.

Creating onResume is fine, as long as the proximity alerts are removed / destroyed every time the application is paused onPause().

Otherwise, create it once in your main / navigation activity. In this case, the receiver will continue to fire even when the app is in background (which may [alarm app] or may not [display changes, for example] be the desired behavior).