android - get location requestLocationUpdates service? -
i try make locationing service somehow cant make work. lat , lng null.
i had exceptions on locmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, loclistener);
if (network_enabled) { locmanager.requestlocationupdates(locationmanager.network_provider,0,0,loclistener);
my code :
package com.example.outmane.training; import android.app.service; import android.content.context; import android.content.intent; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.os.handler; import android.os.ibinder; import android.util.log; import android.widget.toast; public class myservice extends service { private locationmanager locmanager; private locationlistener loclistener = new mylocationlistener(); static final double earth_radius = 6371.00; private boolean gps_enabled = false; private boolean network_enabled = false; private handler handler = new handler(); thread t; @override public ibinder onbind(intent intent) {return null;} @override public void oncreate() {} @override public void ondestroy() {} @override public void onstart(intent intent, int startid) {} @override public int onstartcommand(intent intent, int flags, int startid){ toast.maketext(getbasecontext(), "service started", toast.length_short).show(); final runnable r = new runnable() { public void run() { log.v("debug", "hello"); location(); handler.postdelayed(this, 5000); } }; handler.postdelayed(r, 5000); return start_sticky; } public void location(){ locmanager = (locationmanager) getsystemservice(context.location_service); try{ gps_enabled = locmanager.isproviderenabled(locationmanager.gps_provider); } catch(exception ex){} try{ network_enabled = locmanager.isproviderenabled(locationmanager.network_provider); } catch(exception ex){} log.v("debug", "in on create.. 2"); if (gps_enabled) { locmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, loclistener); log.v("debug", "enabled.."); } if (network_enabled) { locmanager.requestlocationupdates(locationmanager.network_provider,0,0,loclistener); log.v("debug", "disabled.."); } log.v("debug", "in on create..3"); } private class mylocationlistener implements locationlistener { double lat_old=0.0; double lon_old=0.0; double lat_new; double lon_new; double time=10; double speed=0.0; @override public void onlocationchanged(location location) { log.v("debug", "in onlocation changed.."); if(location!=null){ locmanager.removeupdates(loclistener); //string speed = "device speed: " +location.getspeed(); lat_new=location.getlongitude(); lon_new =location.getlatitude(); string longitude = "longitude: " +location.getlongitude(); string latitude = "latitude: " +location.getlatitude(); double distance =calculationbydistance(lat_new, lon_new, lat_old, lon_old); speed = distance/time; toast.maketext(getapplicationcontext(), longitude+"\n"+latitude+"\ndistance is: " +distance+"\nspeed is: "+speed , toast.length_short).show(); lat_old=lat_new; lon_old=lon_new; } } @override public void onproviderdisabled(string provider) {} @override public void onproviderenabled(string provider) {} @override public void onstatuschanged(string provider, int status, bundle extras) {} } public double calculationbydistance(double lat1, double lon1, double lat2, double lon2) { double radius = earth_radius; double dlat = math.toradians(lat2-lat1); double dlon = math.toradians(lon2-lon1); double = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.toradians(lat1)) * math.cos(math.toradians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2); double c = 2 * math.asin(math.sqrt(a)); return radius * c; } }
try using fused loaction provider. take care of heavy stuff. , callbacks when location available.
http://developer.android.com/training/location/retrieve-current.html
Comments
Post a Comment