java - How to refresh my current latitude longitube at background in android -
tell me how referesh current latitude , longitude in activity @ every 5 min
public class mapsactivity extends fragmentactivity implements onmapreadycallback, googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener,locationlistener{ private googlemap mmap; private googleapiclient mgoogleapiclient; private locationrequest mlocationrequest; private marker mcurrlocationmarker; private textview mlatitude, mlongitude; private button mnextbtn; private location location; private int minterval = 5000; private static final string tag = "broadcasttest"; private intent intent; handler mhandler; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_maps); mlatitude = (textview) findviewbyid(r.id.tv_lat); mlongitude = (textview) findviewbyid(r.id.tv_long); mnextbtn = (button) findviewbyid(r.id.btn_next); intent = new intent(this, broadcastservice.class); mnextbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent = new intent(mapsactivity.this, latlonglistactivity.class); startactivity(intent); } }); // obtain supportmapfragment , notified when map ready used. supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this); startrepeatingtask(); mhandler = new handler(); /*thread t = new thread() { @override public void run() { try { while (!isinterrupted()) { thread.sleep(10000); runonuithread(new runnable() { @override public void run() { // update textview here! toast.maketext(getapplicationcontext(),"update",toast.length_short).show(); } }); } } catch (interruptedexception e) { } } }; t.start();*/ } @override public void onmapready(googlemap googlemap) { mmap = googlemap; // add marker in sydney , move camera mmap.setmaptype(googlemap.map_type_hybrid); //initialize google play services if (android.os.build.version.sdk_int >= build.version_codes.m) { if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) == packagemanager.permission_granted) { buildgoogleapiclient(); mmap.setmylocationenabled(true); } } else { buildgoogleapiclient(); mmap.setmylocationenabled(true); } } runnable mstatuschecker = new runnable() { @override public void run() { try { onlocationchanged(location); toast.maketext(getapplicationcontext(),"update",toast.length_short).show(); } { mhandler.postdelayed(mstatuschecker, minterval); // set minterval = 300000 } } }; void startrepeatingtask() { mstatuschecker.run(); } void stoprepeatingtask() { mhandler.removecallbacks(mstatuschecker); } public void onlocationchanged(location location) { // called when listener notified location update gps double currentlatitude = location.getlatitude(); double currentlogitude = location.getlongitude(); latlng latlng = new latlng(currentlatitude, currentlogitude); markeroptions markeroptions = new markeroptions(); markeroptions.position(latlng); markeroptions.title("current position"); markeroptions.icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_magenta)); mcurrlocationmarker = mmap.addmarker(markeroptions); //move map camera mmap.movecamera(cameraupdatefactory.newlatlng(latlng)); mmap.animatecamera(cameraupdatefactory.zoomto(11)); //move map camera mmap.movecamera(cameraupdatefactory.newlatlng(latlng)); mmap.animatecamera(cameraupdatefactory.zoomto(11)); //stop location updates if (mgoogleapiclient != null) { locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, (locationlistener) this); string lat = string.valueof(location.getlatitude()); string longi = string.valueof(location.getlongitude()); mlatitude.settext("latitude : " + lat); mlongitude.settext("logitude : " + longi); sharedpreferences sp = getsharedpreferences("key", 0); sharedpreferences.editor sedt = sp.edit(); sedt.putstring("textvalue", mlatitude.gettext().tostring()); sedt.putstring("txtopertaive", mlongitude.gettext().tostring()); sedt.commit(); } } public void onproviderdisabled(string provider) { // called when gps provider turned off (user turning off gps on phone) } public void onproviderenabled(string provider) { // called when gps provider turned on (user turning on gps on phone) } public void onstatuschanged(string provider, int status, bundle extras) { // called when status of gps provider changes } private void buildgoogleapiclient() { mgoogleapiclient = new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); mgoogleapiclient.connect(); } @override public void onconnected(@nullable bundle bundle) { mlocationrequest = new locationrequest(); mlocationrequest.setinterval(1000); mlocationrequest.setfastestinterval(1000); mlocationrequest.setpriority(locationrequest.priority_balanced_power_accuracy); if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) == packagemanager.permission_granted) { locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest,this); } } @override public void onconnectionsuspended(int i) { } @override public void onconnectionfailed(@nonnull connectionresult connectionresult) { } public static final int my_permissions_request_location = 99; public boolean checklocationpermission(){ if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted) { // asking user if explanation needed if (activitycompat.shouldshowrequestpermissionrationale(this, manifest.permission.access_fine_location)) { // show expanation user *asynchronously* -- don't block // thread waiting user's response! after user // sees explanation, try again request permission. //prompt user once explanation has been shown activitycompat.requestpermissions(this, new string[]{manifest.permission.access_fine_location}, my_permissions_request_location); } else { // no explanation needed, can request permission. activitycompat.requestpermissions(this, new string[]{manifest.permission.access_fine_location}, my_permissions_request_location); } return false; } else { return true; } }
my latitude , longitude set 1 time. tell how refresh every 5 min become refresh.please...
fatal exception: main process: com.currentlocationdemo, pid: 15127 java.lang.classcastexception: com.currentlocationdemo.mapsactivity cannot cast com.google.android.gms.location.locationlistener @ com.currentlocationdemo.mapsactivity.onconnected(mapsactivity.java:228) @ com.google.android.gms.common.internal.zzl.zzo(unknown source) @ com.google.android.gms.internal.zzpy.zzm(unknown source) @ com.google.android.gms.internal.zzpw.zzapp(unknown source) @ com.google.android.gms.internal.zzpw.onconnected(unknown source) @ com.google.android.gms.internal.zzqa.onconnected(unknown source) @ com.google.android.gms.internal.zzpp.onconnected(unknown source) @ com.google.android.gms.common.internal.zzk$1.onconnected(unknown source) @ com.google.android.gms.common.internal.zzd$zzj.zzasd(unknown source) @ com.google.android.gms.common.internal.zzd$zza.zzc(unknown source) @ com.google.android.gms.common.internal.zzd$zza.zzv(unknown source) @ com.google.android.gms.common.internal.zzd$zze.zzasf(unknown source) @ com.google.android.gms.common.internal.zzd$zzd.handlemessage(unknown source) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5930) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1405) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1200)
08-24 14:50:14.124 15127-15762/com.currentlocationdemo w/dynamitemodule: local module descriptor class com.google.android.gms.googlecertificates not found.
place following code in code:
runnable mstatuschecker = new runnable() { @override public void run() { try { <call location fetching method here || call location.get... methods here> } { mhandler.postdelayed(mstatuschecker, minterval); // set minterval = 300000 } } }; void startrepeatingtask() { mstatuschecker.run(); } void stoprepeatingtask() { mhandler.removecallbacks(mstatuschecker); }
Comments
Post a Comment