i thought super easy - taking waaaay longer should.
i'm trying gps coordinates go logcat, that's literally it.
the intent fires fine. "intent fired" message appears every time. application refuses go location changed event.
if take , let me know i'm failing, super appreciated.
public class myreceiver extends intentservice implements locationlistener { private locationmanager locationmanager; public myreceiver() { super("myreceiver"); } protected void onhandleintent(intent intent) { log.i("debugme", "intent fired"); locationmanager = (locationmanager) getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, this); locationmanager.requestlocationupdates(locationmanager.network_provider, 0, 0, this); } public void onlocationchanged(location location) { string lat = string.valueof(location.getlatitude()); string lon = string.valueof(location.getlongitude()); log.i("debugme", lat + " " + lon); locationmanager.removeupdates(this); } public void onproviderdisabled(string provider) { log.d("debugme ", "disabled"); } public void onproviderenabled(string provider) { log.d("debugme","enabled"); } public void onstatuschanged(string provider, int status, bundle extras) { log.d("debugme","status"); } }
thank you
well, location manager works asynchronously, , intentservice
implemented in such way, when onhandleintent(intent intent)
returns, service stops itself. means it's destroyed before location can provided onlocationchanged(location location)
method. if want use intentservice
need block onhandleintent(intent intent)
method returning before have location, or use standard service
, execute stopself
when obtain location
object.
public class myreceiver extends service implements locationlistener { private locationmanager locationmanager; @override public int onstartcommand(intent intent, int flags, int startid) { onhandleintent(intent); return start_not_sticky; } protected void onhandleintent(intent intent) { log.i("debugme", "intent fired"); locationmanager = (locationmanager) getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, this); locationmanager.requestlocationupdates(locationmanager.network_provider, 0, 0, this); } public void onlocationchanged(location location) { string lat = string.valueof(location.getlatitude()); string lon = string.valueof(location.getlongitude()); log.i("debugme", lat + " " + lon); locationmanager.removeupdates(this); // stop service stopself(); } public void onproviderdisabled(string provider) { log.d("debugme ", "disabled"); } public void onproviderenabled(string provider) { log.d("debugme", "enabled"); } public void onstatuschanged(string provider, int status, bundle extras) { log.d("debugme", "status"); } @override public ibinder onbind(intent intent) { return null; } }
keep in mind implementation won't run in separate thread default, intentservice would.
Comments
Post a Comment