broadcastreceiver - set multiple Proximity Alerts using broadcast receiver android -


consider 3 locations home, office , playground. whenever entering , exiting location shows message alert. main activity add multiple proximity points mainactivity.java

 protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);     setsupportactionbar(toolbar);      floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab);     fab.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             snackbar.make(view, "replace own action", snackbar.length_long)                     .setaction("action", null).show();         }     });     locationmanager = (locationmanager) getsystemservice(context.location_service);      latitudeedittext = (edittext) findviewbyid(r.id.point_latitude);     longitudeedittext = (edittext) findviewbyid(r.id.point_longitude);     addalertbutton = (button) findviewbyid(r.id.add_alert_button);     double latitude = 43.039, latitude1 = 43.039;     double longitude = 60.238, longitude1 = 60.239;     addproximityalert(latitude, longitude);     addproximityalert(latitude1, longitude1);      toast.maketext(getapplicationcontext(), "alert added", toast.length_short).show(); }  private void addproximityalert(double latitude,double longitude) {       intent intent = new intent(prox_alert_intent);     intent.putextra("lat",latitude);     intent.putextra("long",longitude);     pendingintent proximityintent = pendingintent.getbroadcast(this, (int) system.currenttimemillis(), intent,pendingintent.flag_cancel_current);     toast.maketext(this,long.tostring(system.currenttimemillis()),toast.length_long).show();     if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) {          return;     }     locationmanager.addproximityalert(latitude,             longitude,             point_radius,             prox_alert_expiration,             proximityintent     );     intentfilter filter = new intentfilter(prox_alert_intent);     registerreceiver(new proximityintentreceiver(), filter);  } 

//this pending intent receiver notify entering , existing alerts pendingintentreceiver.java

    public class proximityintentreceiver extends broadcastreceiver {     private static final int notification_id = 1000;     double lat,lng;     string  d="";     @override     public void onreceive(context context, intent intent) {         string key = locationmanager.key_proximity_entering;         boolean entering = intent.getbooleanextra(key, false);         lat=intent.getdoubleextra("lat",0.00);         lng=intent.getdoubleextra("long",0.00);         if (entering) {             log.d(getclass().getsimplename(), "entering");             d="entering region"+double.tostring(lat)+double.tostring(lng);             toast.maketext(context, d, toast.length_short).show();         }else {             log.d(getclass().getsimplename(), "exiting");             d= "exiting region"+double.tostring(lat)+double.tostring(lng);             toast.maketext(context,d, toast.length_short).show();          }          notificationmanager notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service);          intent notificationintent = new intent(context, mainactivity.class);         pendingintent pendingintent = pendingintent.getactivity(context, (int) system.currenttimemillis(), notificationintent, 0);         notification n  = new notification.builder(context)                 .setcontenttitle("proximity")                 .setcontenttext("subject: "+d)                 .setsmallicon(r.drawable.hell)                 .setcontentintent(pendingintent)                 .setautocancel(true)                 .build();         notificationmanager.notify((int) system.currenttimemillis(), n);      }   } 

try this:

intent notificationintent = new intent(context, mainactivity.class); notificationintent.setaction(long.tostring(system.currenttimemillis()));  taskstackbuilder stackbuilder = taskstackbuilder.create(context); stackbuilder.addparentstack(mainactivity.class); stackbuilder.addnextintent(intent);  pendingintent pendingintent = stackbuilder.getpendingintent(0, pendingintent.flag_update_current);  notification n  = new notification.builder(context)         .setcontenttitle("proximity")         .setcontenttext("subject: "+d)         .setsmallicon(r.drawable.hell)         .setcontentintent(pendingintent)         .setautocancel(true)         .build(); notificationmanager.notify((int) system.currenttimemillis(), n); 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

android - CoordinatorLayout, FAB and container layout conflict -