alarmmanager - How to run an activity once in 3o mins forever android? -


hi want code repeated forever, can body me pls trying lot not explanantion cant understand how repeate once half hour using alarm method or timer. can me? , dont need service 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();         }     } } 

from code, looks trying fetch , log location , nearby stations every 30 minutes. on complicating simple thing.

in opinion, should move code location provider service keep running in background. in service, register location updates every 30 min (as done already). every 30 min, android call locationlistener callback methods , can perform action accordingly (save file, display notification, send message acivity update ui).

also user perspective , suggested ergo, shouldn't pop toast every 30 min notifying user disabled gps.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -