android - GPS Location Always Shows Location as zero -
i sending location of user email on button click. doinbackground of asynctask given below.
@override protected boolean doinbackground(string... arg0) { // todo auto-generated method stub boolean status = false; try { double latitude=0; double longitude=0; gpstracker gps=new gpstracker(mainactivity.this); if(gps.cangetlocation()){ latitude=gps.getlatitude(); longitude=gps.getlongitude(); } string mapurl= "https://www.google.co.in/maps/@"+latitude+","+longitude+",17z"; gmailsender sender = new gmailsender( "myemail@gmail.com", "password", getapplicationcontext()); status = sender.sendmail(" subject ", "\nlocation : "+mapurl,"myemail@gmail.com", "anothermail@gmail.com"); } catch (exception e) { } return status; }
but returns location 0,0
gpstracker.java
locationmanager = (locationmanager) mcontext .getsystemservice(location_service); // getting gps status isgpsenabled = locationmanager .isproviderenabled(locationmanager.gps_provider); // getting network status isnetworkenabled = locationmanager .isproviderenabled(locationmanager.network_provider); if (!isgpsenabled && !isnetworkenabled) { // no network provider enabled } else { this.cangetlocation = true; if (isnetworkenabled) { locationmanager.requestlocationupdates( locationmanager.network_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("network", "network enabled"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.network_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } // if gps enabled lat/long using gps services if (isgpsenabled) { locationmanager.requestlocationupdates( locationmanager.gps_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("gps", "gps enabled"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.gps_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } }
the log shows
05-16 22:01:30.449: w/system.err(12459): java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare() 05-16 22:01:30.489: w/system.err(12459): @ android.os.handler.<init>(handler.java:121) 05-16 22:01:30.489: w/system.err(12459): @ android.location.locationmanager$listenertransport$1.<init>(locationmanager.java:198) 05-16 22:01:30.489: w/system.err(12459): @ android.location.locationmanager$listenertransport.<init>(locationmanager.java:198) 05-16 22:01:30.499: w/system.err(12459): @ android.location.locationmanager._requestlocationupdates(locationmanager.java:604) 05-16 22:01:30.499: w/system.err(12459): @ android.location.locationmanager.requestlocationupdates(locationmanager.java:471) 05-16 22:01:30.509: w/system.err(12459): @ com.package.utils.gpstracker.getlocation(gpstracker.java:65) 05-16 22:02:07.309: w/system.err(12459): @ com.package.utils.gpstracker.getlocation(gpstracker.java:65) 05-16 22:02:07.309: w/system.err(12459): @ com.package.utils.gpstracker.<init>(gpstracker.java:44) 05-16 22:02:07.309: w/system.err(12459): @ com.webnamaste.ulrasoundindia.mainactivity$sendmailtask.doinbackground(mainactivity.java:255) 05-16 22:02:07.319: w/system.err(12459): @ com.package.ulrasoundindia.mainactivity$sendmailtask.doinbackground(mainactivity.java:1) 05-16 22:02:07.319: w/system.err(12459): @ android.os.asynctask$2.call(asynctask.java:264) 05-16 22:02:07.319: w/system.err(12459): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 05-16 22:02:07.319: w/system.err(12459): @ java.util.concurrent.futuretask.run(futuretask.java:137) 05-16 22:02:07.319: w/system.err(12459): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:208) 05-16 22:02:07.339: w/system.err(12459): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 05-16 22:02:07.339: w/system.err(12459): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 05-16 22:02:07.349: w/system.err(12459): @ java.lang.thread.run(thread.java:856)
method doinbackground
of asynctask
executed on background thread, it's not main (ui) thread. here can have 3 solutions:
- move getting gps coordinates procedure
onpreexecute
, pass resultdoinbackground
- call
runonuithread
@doinbackground
invokinggpstracker
, ugly solution. - use combination of
looper.prepare();
,looper.loop();
take here
Comments
Post a Comment