I Couldn't Reach Change Location Value in Android -
i'm sorry mine simple question i'm newbie topic. have location class android , reach mine location toast message couldn't reach mine location value other class. example;
my location class
import android.app.alertdialog; import android.content.context; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.widget.toast; public class mylocation{ private locationmanager locationmanager; locationlistener locationlistener; private context ourcontext; public mylocation(context context){ this.ourcontext = context; locationmanager = (locationmanager) context.getsystemservice(context.location_service); locationlistener = new mylocationlistener(); boolean gps = locationmanager.isproviderenabled(locationmanager.gps_provider); if(!gps){ locationmanager.requestlocationupdates(locationmanager.network_provider, 6000, 100, locationlistener); alertdialog alertdialog = new alertdialog.builder(context).create(); alertdialog.settitle("gps disabled"); alertdialog.setmessage("you can enable gps settings"); alertdialog.seticon(r.drawable.ic_launcher); alertdialog.show(); } else { locationmanager.requestlocationupdates(locationmanager.gps_provider, 6000, 100, locationlistener); } } public class mylocationlistener implements locationlistener{ @override public void onlocationchanged(location location) { // todo auto-generated method stub bundle bundle = new bundle(); lat = location.getlatitude(); lon = location.getlongitude(); toast.maketext(ourcontext, lat +" ve "+ lon, toast.length_long).show(); bundle.putstring("lat", lat.tostring()); bundle.putstring("lon", lon.tostring()); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } public string getlat(){ return lat.tostring(); } public string getlon(){ return lon.tostring(); } private double lat; private double lon; }
and call
mylocation mylocation = new mylocation(getactivity()); runnable runnable = new runnable() { @override public void run() { // todo auto-generated method stub while(bundle.getstring("lon") != null){ tv1.settext("lon : " + bundle.getstring("lon")); tv2.settext("lat : " + bundle.getstring("lat")); } } }; new thread(runnable).start();
but every time bundle return null value. can ?
because construct new bundle can retrieve datas saved in activity.
you can implement following :
public class mylocationlistener implements locationlistener{ bundle bundle = new bundle(); public bundle getbundle(){ return bundle; } @override public void onlocationchanged(location location) { // todo auto-generated method stub lat = location.getlatitude(); lon = location.getlongitude(); toast.maketext(ourcontext, lat +" ve "+ lon, toast.length_long).show(); bundle.putstring("lat", lat.tostring()); bundle.putstring("lon", lon.tostring()); } //other method } public class mylocation{ private locationmanager locationmanager; locationlistener locationlistener; private context ourcontext; public mylocation(context context){ this.ourcontext = context; locationmanager = (locationmanager) context.getsystemservice(context.location_service); locationlistener = new mylocationlistener(); boolean gps = locationmanager.isproviderenabled(locationmanager.gps_provider); if(!gps){ locationmanager.requestlocationupdates(locationmanager.network_provider, 6000, 100, locationlistener); alertdialog alertdialog = new alertdialog.builder(context).create(); alertdialog.settitle("gps disabled"); alertdialog.setmessage("you can enable gps settings"); alertdialog.seticon(r.drawable.ic_launcher); alertdialog.show(); } else { locationmanager.requestlocationupdates(locationmanager.gps_provider, 6000, 100, locationlistener); } public bundle getbundle(){ if(locationlistener != null) return locationlistener.getbundle(); else return null; } }
then :
bundle bundle = mylocation.getbundle(); if(bundle != null){ tv1.settext("lon : " + bundle.getstring("lon")); tv2.settext("lat : " + bundle.getstring("lat")); }
edit :
change private locationlistener locationlistener;
with private mylocationlistener locationlistener;
Comments
Post a Comment