android - proximity Alert not fired -
this first question here , search answer not found. build simple app that current position , give alert if i'm stand on specific position, alert not fired, please help. here code :
mainactivity:
package com.lda.notyalert; public class mainactivity extends activity { proximityintentreceiver proximityintentreceiver; private static final string proximity_alert = "com.lda.notyalert.proximityintentreceiver"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); locationmanager locationmanager; string svcname = context.location_service; locationmanager = (locationmanager) getsystemservice(svcname); criteria criteria = new criteria(); criteria.setaccuracy(criteria.accuracy_fine); criteria.setpowerrequirement(criteria.power_low); criteria.setaltituderequired(false); criteria.setbearingrequired(false); criteria.setspeedrequired(false); criteria.setcostallowed(true); string provider = locationmanager.getbestprovider(criteria, true); location l = locationmanager.getlastknownlocation(provider); updatewithnewlocation(l); locationmanager.requestlocationupdates(provider, 2000, 10, locationlistener); setproximityalert(); intentfilter filter = new intentfilter(proximity_alert); proximityintentreceiver = new proximityintentreceiver(); registerreceiver(proximityintentreceiver, filter); } private void setproximityalert() { string locservice = context.location_service; locationmanager locationmanager; locationmanager = (locationmanager) getsystemservice(locservice); double lat = 37.422006; double lng = -122.084095; float radius = 100f; // meters long expiration = -1; // not expire intent intent = new intent(proximity_alert); pendingintent proximityintent = pendingintent.getbroadcast(this, -1, intent, 0); locationmanager.addproximityalert(lat, lng, radius, expiration, proximityintent); } private void updatewithnewlocation(location location) { textview mylocationtext; mylocationtext = (textview) findviewbyid(r.id.textview1); string latlongstring = "no location found"; if (location != null) { double lat = location.getlatitude(); double lng = location.getlongitude(); latlongstring = "lat:" + lat + "\nlong:" + lng; } mylocationtext.settext("your current position is:\n" + latlongstring); } private final locationlistener locationlistener = new locationlistener() { public void onlocationchanged(location location) { updatewithnewlocation(location); } public void onproviderdisabled(string provider) { } public void onproviderenabled(string provider) { } public void onstatuschanged(string provider, int status, bundle extras) { } }; }
this proximityintentreceiver extends broadcastreceiver;
package com.lda.notyalert; public class proximityintentreceiver extends broadcastreceiver { private static final int notification_id = 1000; @suppresswarnings("deprecation") @override public void onreceive(context context, intent intent) { string key = locationmanager.key_proximity_entering; boolean entering = intent.getbooleanextra(key, false); if (entering) { log.d(getclass().getsimplename(), "entering"); } else { log.d(getclass().getsimplename(), "exiting"); } notificationmanager notificationmanager = (notificationmanager) context .getsystemservice(context.notification_service); pendingintent pendingintent = pendingintent.getactivity(context, 0, null, 0); notification notification = createnotification(); notification.setlatesteventinfo(context, "proximity alert!", "you near point of interest.", pendingintent); notificationmanager.notify(notification_id, notification); } 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_lights; notification.ledargb = color.white; notification.ledonms = 1500; notification.ledoffms = 1500; return notification; } }
Comments
Post a Comment