android - How to run a background process in service method completely? -
hi can body show me how run code in service without activity, have done code in activity dont want application need in service display on service thank have tried activity displaying once in 30 mins.
this code:
public class gps extends activity implements locationlistener { locationmanager manager; string closeststation; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); { calendar cur_cal = calendar.getinstance(); cur_cal.settimeinmillis(system.currenttimemillis()); cur_cal.add(calendar.minute, 15); log.d("testing", "calender set time:" + cur_cal.gettime()); intent intent = new intent(gps.this, gps_back_process.class); pendingintent pintent = pendingintent.getservice(gps.this, 0, intent, 0); alarmmanager alarm_manager = (alarmmanager) getsystemservice(context.alarm_service); alarm_manager.setrepeating(alarmmanager.rtc_wakeup, cur_cal.gettimeinmillis(), 1000 * 60 * 15, pintent); alarm_manager.set(alarmmanager.rtc, cur_cal.gettimeinmillis(), pintent); log.d("testing", "alarm manager set"); toast.maketext(this, "gps_back_process.oncreate()", toast.length_long).show(); } 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..."); } manager.requestlocationupdates(providername, 1000*60*30 , 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 gps network id : " + ""); } } @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(environment.getexternalstoragedirectory() + "/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)); simpledateformat sdf = new simpledateformat("hh:mm, dd/mm/yyyy"); string currentdateandtime = sdf.format(new date()); // text+=","+currentdateandtime; buf.append(text + "," + currentdateandtime); buf.newline(); buf.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
1.extends service class instead of activity class and
2. put code oncreate()
3.add service manefest.
4.call service activity once
intent service = new intent(context, localservice.class); context.startservice(service); follow this tutorial.
Comments
Post a Comment