service - Android - remove Proximity Alert after notification -
what trying have proximity alert service triggers notification once when step inside radius (without stopping service). code triggers notifications every time step inside radius , every time step outside radius. i've been trying booleans , removeproximityalert, no success. ideas?
import android.app.notification; import android.app.notificationmanager; import android.app.pendingintent; import android.app.service; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.location.locationmanager; import android.os.ibinder; import android.util.log; import android.widget.toast; public class proximityservice extends service { private string prox_alert_intent = "com.example.proximityalert"; private broadcastreceiver locationreminderreceiver; private locationmanager locationmanager; private pendingintent proximityintent; @override public void oncreate() { locationreminderreceiver = new proximityintentreceiver(); locationmanager = (locationmanager) getsystemservice(context.location_service); double lat = 55.586568; double lng = 13.0459; float radius = 1000; long expiration = -1; intentfilter filter = new intentfilter(prox_alert_intent); registerreceiver(locationreminderreceiver, filter); intent intent = new intent(prox_alert_intent); intent.putextra("alert", "test zone"); proximityintent = pendingintent.getbroadcast(this, 0, intent, pendingintent.flag_one_shot); locationmanager.addproximityalert(lat, lng, radius, expiration, proximityintent); } @override public void ondestroy() { toast.maketext(this, "proximity service stopped", toast.length_long).show(); try { unregisterreceiver(locationreminderreceiver); } catch (illegalargumentexception e) { log.d("receiver", e.tostring()); } } @override public void onstart(intent intent, int startid) { toast.maketext(this, "proximity service started", toast.length_long).show(); } @override public ibinder onbind(intent intent) { // todo auto-generated method stub return null; } public class proximityintentreceiver extends broadcastreceiver { private static final int notification_id = 1000; @suppresswarnings("deprecation") @override public void onreceive(context arg0, intent arg1) { string place = arg1.getextras().getstring("alert"); notificationmanager notificationmanager = (notificationmanager) getsystemservice(context.notification_service); pendingintent pendingintent = pendingintent.getactivity(arg0, 0, arg1, 0); notification notification = createnotification(); notification.setlatesteventinfo(arg0, "entering proximity!", "you approaching " + place + " marker.", pendingintent); notificationmanager.notify(notification_id, notification); locationmanager.removeproximityalert(proximityintent); } private notification createnotification() { notification notification = new notification(); notification.icon = r.drawable.ic_launcher; notification.when = system.currenttimemillis(); notification.flags |= notification.flag_auto_cancel; notification.flags |= notification.flag_show_lights; notification.defaults |= notification.default_vibrate; notification.defaults |= notification.default_sound; return notification; } } }
you should remove proximity alert after fires , not recreate again (save flag, variable or, if use sqlite, in db).
removing alerts bit tricky, there 2 steps take , both produce intended result of not (apparently) "firing":
unregister receiver
you may save receiver (or array of receivers) ,
unregister
directly:for (proximityreceiver proximityreceiverlocal:proximityreceiverarray) { context.unregisterreceiver(proximityreceiverlocal); proximityreceiverarray.remove(proximityreceiverlocal); // clear pile }
remove alerts
note: cannot save alert object, must recreate
pendingintent
, submitremoveproximityalert
method. pending intent must have same intent (i.e. sameaction name
) , 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 remove 1 , not other, achieve intended goal of nothing occurring when enter poi's radius, wasting precious battery life , memory.
Comments
Post a Comment