gps - Sending current location as SMS Android Studio -


i'm new android studio.

i'm trying send current location text message when clicking button nothing happens when click.

first, here's xml:

<button android:id="@+id/emergency"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_alignparentstart="true"         android:layout_centervertical="true"         android:layout_marginleft="14dp"         android:layout_marginstart="14dp"         android:background="@drawable/button_selector1"         android:drawableend="@android:drawable/ic_dialog_alert"         android:gravity="center"         android:maxlines="1"         android:paddingleft="15dip"         android:paddingright="15dip" /> 

then, here's mainactivity code:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     requestwindowfeature(window.feature_no_title);     setcontentview(r.layout.ly_home);      emergency = (button) findviewbyid(r.id.emergency);      emergency.setonclicklistener(new view.onclicklistener(){         @override         public void onclick(view v){             gpstracker g = new gpstracker(getapplicationcontext());             location l = g.getlocation();             if (l != null){                 double lat = l.getlatitude();                 double lon = l.getlongitude();                 string message="http://maps.google.com/maps?saddr="+lat+","+lon;                 string number = "xxxxxxxx";                 smsmanager smsmanager = smsmanager.getdefault();                 stringbuffer smsbody = new stringbuffer();                 smsbody.append(uri.parse(message));                 android.telephony.smsmanager.getdefault().sendtextmessage(number, null, smsbody.tostring(), null,null);             }         }     }); } 

and, gpstracker class:

public class gpstracker implements locationlistener{      context context;     public gpstracker(context c){         context = c;     }      public location getlocation(){          locationmanager lm = (locationmanager) context.getsystemservice(context.location_service);         boolean isgpsenabled = lm.isproviderenabled(locationmanager.gps_provider);         if (isgpsenabled){             lm.requestlocationupdates(locationmanager.gps_provider,6000,10,this);             location l = lm.getlastknownlocation(locationmanager.gps_provider);             return l;         }else{             toast.maketext(context,"please enable gps", toast.length_long).show();         }         return null;     }      @override     public void onlocationchanged(location location) {      }      @override     public void onstatuschanged(string provider, int status, bundle extras) {      }      @override     public void onproviderenabled(string provider) {      }      @override     public void onproviderdisabled(string provider) {      } } 

i've given permissions in manifest: fine location, coarse location , receiving , sending text messages. doing wrong?

as per below answer https://stackoverflow.com/a/43199211/6835152

there 2 different ways retrieve location, 1 based on network , based on gps services. certainly, location based on gps services more accurate. according documentation, might take time receive location using gps services. in case, last known location can used available location using getlastknownlocation() method. here, last available location, should change locationmanager.gps_provider locationmanager.network_provider.

and if wish receive accurate, real time location based on gps services, recommend use fusedlocationapi. can find here https://developer.android.com/training/location/receive-location-updates.html


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -