android - Geofences not triggering (pendingintents and broadcastreceiver) -
i've used android tutorial geofences which, obviously, doesn't work when app closed. so, after searching around, i've noticed user b-ryce on used broadcastreceiver geofences triggered if app not active (link question).
i cannot force geofences trigger when move outside/inside registered location. here's procedure:
/** * geolocation library */ mgeolocation = new geolocation(sinstance); /** * mreceiver init */ mreceiver = new receiver(); sinstance.registerreceiver(mreceiver, mreceiver.createintentfilter()); and inside geolocation class:
/* * create pendingintent triggers intentservice in * app when geofence transition occurs. */ protected pendingintent gettransitionpendingintent() { if (mgeopendingintent != null) { return mgeopendingintent; } else { // create explicit intent // intent intent = new intent(mcontext, // receivetransitionsintentservice.class); intent intent = new intent(getclass().getpackage().getname() + ".geofence_receive"); /** * return pendingintent */ return pendingintent.getbroadcast( mcontext, 0, intent, pendingintent.flag_update_current); } } here's how create new geofence:
geofence fence = new geofence.builder() .setrequestid(hashcode) // when entering geofence .settransitiontypes(geofence.geofence_transition_enter | geofence.geofence_transition_exit) .setcircularregion( double.parsedouble(single.getsetting("latitude")), double.parsedouble(single.getsetting("longitude")), float.parsefloat(single.getsetting("radius")) // radius in meters ) .setexpirationduration(geofence.never_expire) .build(); mgeofences.add(fence); array gets populated , call addgeofences method inside geofence class
mgeolocation.addgeofences(mgeofences); androidmanifest.xml reciever class:
<!-- receiver --> <receiver android:name=".receiver" > </receiver> and receiver class should log when geofence triggers
public void onreceive(context context, intent intent) { string action = intent.getaction(); log.d("sfen", "broadcast recieved "+ action +" not needed."); } the problem is, geofence triggering when open map inside app pick locations. closing app (running in background) doesn't trigger , geofence transitions don't trigger anything.
can tell me doing wrong?
you should use intentservice receive geofencing event(not receiver).i wrote demo @ https://github.com/chenjishi/android_location_demo.
1.first define locationclient
private locationclient locationclient; 2.init it
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); int resp = googleplayservicesutil.isgoogleplayservicesavailable(this); if (resp == connectionresult.success) { locationclient = new locationclient(this, this, this); locationclient.connect(); } } 3.when connect success, register geofences
@override public void onconnected(bundle bundle) { arraylist<store> storelist = getstorelist(); if (null != storelist && storelist.size() > 0) { arraylist<geofence> geofencelist = new arraylist<geofence>(); (store store : storelist) { float radius = (float) store.radius; geofence geofence = new geofence.builder() .setrequestid(store.id) .settransitiontypes(geofence.geofence_transition_enter | geofence.geofence_transition_exit) .setcircularregion(store.latitude, store.longitude, radius) .setexpirationduration(geofence.never_expire) .build(); geofencelist.add(geofence); } pendingintent geofencependingintent = pendingintent.getservice(this, 0, new intent(this, geofenceintentservice.class), pendingintent.flag_update_current); locationclient.addgeofences(geofencelist, geofencependingintent, this); } } 4.finally define intentservice receive geofencing event
public class geofenceintentservice extends intentservice { public static final string transition_intent_service = "receivetransitionsintentservice"; public geofenceintentservice() { super(transition_intent_service); } @override protected void onhandleintent(intent intent) { if (locationclient.haserror(intent)) { //todo error process } else { int transitiontype = locationclient.getgeofencetransition(intent); if (transitiontype == geofence.geofence_transition_enter || transitiontype == geofence.geofence_transition_exit) { list<geofence> triggerlist = locationclient.gettriggeringgeofences(intent); (geofence geofence : triggerlist) { generatenotification(geofence.getrequestid(), "address defined"); } } } } private void generatenotification(string locationid, string address) { long when = system.currenttimemillis(); intent notifyintent = new intent(this, mainactivity.class); notifyintent.putextra("id", locationid); notifyintent.putextra("address", address); notifyintent.setflags(intent.flag_activity_new_task | intent.flag_activity_clear_task); pendingintent pendingintent = pendingintent.getactivity(this, 0, notifyintent, pendingintent.flag_update_current); notificationcompat.builder builder = new notificationcompat.builder(this) .setsmallicon(r.drawable.dac_logo) .setcontenttitle(locationid) .setcontenttext(address) .setcontentintent(pendingintent) .setautocancel(true) .setdefaults(notification.default_sound) .setwhen(when); notificationmanager notificationmanager = (notificationmanager) getsystemservice(context.notification_service); notificationmanager.notify((int) when, builder.build()); } }
Comments
Post a Comment