Android, Get GPS location when clicking a button -


i trying learn how use gps service in android , that, developing program show actual gps location when user press button.

but whenever press it, 0.0...

i think piece of code not working fine:

locationmanager mlocmanager=null;     locationlistener mloclistener;     mlocmanager = (locationmanager)getsystemservice(context.location_service);     mloclistener = new mylocationlistener();     mlocmanager.requestlocationupdates( locationmanager.gps_provider, 0, 0, mloclistener);       button.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             log.d(logtag,"onclickbutton");                 firstcoord.settext("");             log.d(logtag,double.tostring(mylocationlistener.latitude));             log.d(logtag, "aaaaaaaa");             if(mylocationlistener.latitude>0)                 {                     coordlat1 = mylocationlistener.latitude;                     coordlon1 = mylocationlistener.longitude;                     firstcoord.settext(double.tostring(coordlat1) + ' ' + double.tostring(coordlon1));                 }                 else                 {                     alertdialog.builder alert = new alertdialog.builder(home_screen.this);                     alert.settitle("wait");                     alert.setmessage("gps en progress, wait.");                     alert.setpositivebutton("ok", null);                     alert.show();                 }         }     }); 

the first log after onclickbutton, shows 0.0 if use google maps app, shows mobile phone is. problem think not getting coordinates.

this mylocationlistener class: package com.gps.distance;

import android.location.location; import android.location.locationlistener; import android.os.bundle; import android.util.log;  public class mylocationlistener implements locationlistener {  private static final string logtag = "logsandroid"; public static double latitude; public static double longitude;  @override public void onlocationchanged(location loc) {     loc.getlatitude();     loc.getlongitude();     latitude=loc.getlatitude();     longitude=loc.getlongitude();     log.d(logtag, "take pos"); }  @override public void onproviderdisabled(string provider) {     //print "currently gps disabled";     log.d(logtag, "gps disabled"); } @override public void onproviderenabled(string provider) {     //print "gps got enabled";     log.d(logtag, "gps enabled"); } @override public void onstatuschanged(string provider, int status, bundle extras) { } } 

onlocationchanged never used. if disable or enable gps, logs appear onlocationchanged not... never.


Comments