gps - How to store a value from a textbox android? -
i developing gps program in getting gps values every 5 minutes, , working great, have store values get. has been refreshed every 5 minutes , have 1 text view deletes old values when new 1 refreshed.
this code.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intent intent = new intent("android.location.gps_enabled_change"); intent.putextra("enabled", true); this.sendbroadcast(intent); string provider = settings.secure.getstring(getcontentresolver(), settings.secure.location_providers_allowed); if(!provider.contains("gps")){ //if gps disabled final intent poke = new intent(); poke.setclassname("com.android.settings", "com.android.settings.widget.settingsappwidgetprovider"); poke.addcategory(intent.category_alternative); poke.setdata(uri.parse("3")); this.sendbroadcast(poke); } { //initialize location manager manager = (locationmanager) getsystemservice(context.location_service); //check if gps enabled //if not, notify user toast if (!manager.isproviderenabled(locationmanager.gps_provider)); else { //get location provider location manager //empty criteria searches through providers , returns best 1 string providername = manager.getbestprovider(new criteria(), true); location location = manager.getlastknownlocation(providername); textview tv = (textview)findviewbyid(r.id.locationresults); if (location != null) { tv.settext(location.getlatitude() + " latitude, " + location.getlongitude() + " longitude"); } else { tv.settext("last known location not found. waiting updated location..."); } //sign notified of location updates every 15 seconds - production code should @ least minute manager.requestlocationupdates(providername, 60000, 1, this); } } } @override public void onlocationchanged(location location) { textview tv = (textview)findviewbyid(r.id.locationresults); if (location != null) { tv.settext(location.getlatitude() + " latitude, " + location.getlongitude() + " longitude"); } else { tv.settext("problem getting location"); } } @override public void onproviderdisabled(string arg0) {} @override public void onproviderenabled(string arg0) {} @override public void onstatuschanged(string arg0, int arg1, bundle arg2) {} // find closest bart station public string findclosestbart(location loc) { double lat = loc.getlatitude(); double lon = loc.getlongitude(); double curstatlat = 0; double curstatlon = 0; double shortestdistsofar = double.positive_infinity; double curdist; string curstat = null; string closeststat = null; //sort through stations // write sort of loop using api. curdist = math.sqrt( ((lat - curstatlat) * (lat - curstatlat)) + ((lon - curstatlon) * (lon - curstatlon)) ); if (curdist < shortestdistsofar) { closeststat = curstat; } return closeststat; }
thank you.
you can store textview's value file persistance storage. study answer properly, adding file store method in existing code,
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intent intent = new intent("android.location.gps_enabled_change"); intent.putextra("enabled", true); this.sendbroadcast(intent); string provider = settings.secure.getstring(getcontentresolver(), settings.secure.location_providers_allowed); if(!provider.contains("gps")){ //if gps disabled final intent poke = new intent(); poke.setclassname("com.android.settings", "com.android.settings.widget.settingsappwidgetprovider"); poke.addcategory(intent.category_alternative); poke.setdata(uri.parse("3")); this.sendbroadcast(poke); } { //initialize location manager manager = (locationmanager) getsystemservice(context.location_service); //check if gps enabled //if not, notify user toast if (!manager.isproviderenabled(locationmanager.gps_provider)); else { //get location provider location manager //empty criteria searches through providers , returns best 1 string providername = manager.getbestprovider(new criteria(), true); location location = manager.getlastknownlocation(providername); textview tv = (textview)findviewbyid(r.id.locationresults); if (location != null) { tv.settext(location.getlatitude() + " latitude, " + location.getlongitude() + " longitude"); } else { tv.settext("last known location not found. waiting updated location..."); } //sign notified of location updates every 15 seconds - production code should @ least minute manager.requestlocationupdates(providername, 60000, 1, this); } } } @override public void onlocationchanged(location location) { textview tv = (textview)findviewbyid(r.id.locationresults); if (location != null) { tv.settext(location.getlatitude() + " latitude, " + location.getlongitude() + " longitude"); // have added line appenddata ( location.getlatitude() + " latitude, " + location.getlongitude() + " longitude" ); } else { tv.settext("problem getting location"); } } @override public void onproviderdisabled(string arg0) {} @override public void onproviderenabled(string arg0) {} @override public void onstatuschanged(string arg0, int arg1, bundle arg2) {} // find closest bart station public string findclosestbart(location loc) { double lat = loc.getlatitude(); double lon = loc.getlongitude(); double curstatlat = 0; double curstatlon = 0; double shortestdistsofar = double.positive_infinity; double curdist; string curstat = null; string closeststat = null; //sort through stations // write sort of loop using api. curdist = math.sqrt( ((lat - curstatlat) * (lat - curstatlat)) + ((lon - curstatlon) * (lon - curstatlon)) ); if (curdist < shortestdistsofar) { closeststat = curstat; } return closeststat; } // method write in file public void appenddata(string text) { file datafile = new file("sdcard/gpsdata.txt"); if (!datafile.exists()) { try { datafile.createnewfile(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } try { //bufferedwriter performance, true set append file flag bufferedwriter buf = new bufferedwriter(new filewriter(datafile, true)); buf.append(text); buf.newline(); buf.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } }
you need write following permission in androidmanifest.xml
<uses-permission android:name="android.permission.write_external_storage" />
Comments
Post a Comment