Android - remove Proximity Alert after notification

喜夏-厌秋 提交于 2019-12-06 06:46:18

You should remove the proximity alert after it fires and not recreate it again (save some flag, as a variable or, if you use sqlite, in your db).

Removing alerts is a bit tricky, there are 2 steps to take and both will produce the intended result of not (apparently) "firing":

  1. Unregister your receiver

    You may save a receiver (or array of receivers) and unregister it directly:

    for (ProximityReceiver proximityReceiverLocal:proximityReceiverArray) {
        context.unregisterReceiver(proximityReceiverLocal);
        proximityReceiverArray.remove(proximityReceiverLocal); // clear pile
    }
    
  2. Remove alerts

    Note: You cannot save an alert as an object, you must recreate your PendingIntent and submit it to its removeProximityAlert method. The pending intent must have the same intent (i.e. same action name) and the same pending intent id:

    public void removeProximityAlert(int pendingIntentIdIn, String intentActionNameIn) {
        Intent intent = new Intent(intentActionNameIn);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context , pendingIntentIdIn, intent, 0);
        locationManager.removeProximityAlert(pendingIntent);
    }
    

If you remove one and not the other, you will achieve your intended goal of nothing occurring when you enter the POI's radius, but you will be wasting precious battery life and memory.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!