java - getSystemService started by new TimerTask within a service -


hello community,

i'm beginner in programming. tried code snippets out , worked fine. them have problems.

what have:

  • android galaxy s handy
  • indigo service release 2

what do:
every 2 minutes, handy shall location , send information mysql-database via http. said above, each step works fine putting them problem me.

what have done: have created startup screen 2 buttons start , stop service. when close app started service shall continue sending data:

package ars.samsung.de;  import ars.samsung.de.myservice; import ars.samsung.de.r;  import android.os.bundle; import android.app.activity; import android.content.intent; import android.util.log; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button;  public class samsungloc1 extends activity implements onclicklistener {     private static final string tag = "servicesdemo";       button buttonstart, buttonstop;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_samsung_loc1);          buttonstart = (button) findviewbyid(r.id.buttonstart);         buttonstop = (button) findviewbyid(r.id.buttonstop);          buttonstart.setonclicklistener(this);         buttonstop.setonclicklistener(this);      }       public void onclick(view src) {         switch (src.getid()) {         case r.id.buttonstart:           log.d(tag, "onclick: starting service");           startservice(new intent(this, myservice.class));           break;         case r.id.buttonstop:           log.d(tag, "onclick: stopping service");           stopservice(new intent(this, myservice.class));           break;         }       }  } 

so when clicking start button, service myservice.class starts:

package ars.samsung.de;  import java.util.timer; import java.util.timertask; import android.app.service; import android.content.context; import android.content.intent; import android.media.mediaplayer; import android.os.ibinder; import android.util.log; import android.widget.textview; import android.widget.toast; import ars.samsung.de.*;  public class myservice extends service {     private static final string tag = "myservice";     @override     public ibinder onbind(intent intent) {         return null;     }      @override     public void oncreate() {         toast.maketext(this, "my service created", toast.length_long).show();         log.d(tag, "oncreate");     }      @override     public void ondestroy() {         toast.maketext(this, "my service stopped", toast.length_long).show();         log.d(tag, "ondestroy");     }      @override     public void onstart(intent intent, int startid) {         toast.maketext(this, "my service started", toast.length_long).show();         log.d(tag, "onstart");           timer t = new timer();             t.scheduleatfixedrate(new timertask() { //              @override                 public void run() {                     string url = "";                     string output ="";                     //called each time when xxx milliseconds (1 second) (the period parameter)                     // locator start                     mynetworklocator mnl1 = new mynetworklocator();                     mnl1.starter(this); //                  mnl1.starter(context);                     // locator ende                      myhilfsklasse mhk1 = new myhilfsklasse();                     try {                         //output = mhk1.my_geturlcontent_v2();                         url="http://www.mysite.net/test/location/location_push.php?vonhandy=c&mylatitude=1.6&mylongitude=3.2";                         output = mhk1.my_geturlcontent_v3(url);                         log.d(tag, "http done");                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                         log.d(tag, "catch: http");                     }                        }                                 },             //set how long before start calling timertask (in milliseconds)             0,             //set amount of time between each execution (in milliseconds)             120000);             } } 

so service above execute task every 2 minutes via new timertask. first step worked fine send dummy php every 2 minutes.

but want location information now. in code above error @ mnl1.starter(this). error "the method starter(context) in type mynetworklocator not applicable arguments (newtimertask(){})

the mynetworklocator looks this:

package ars.samsung.de;  import android.content.context; import android.location.criteria; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.widget.textview;  public class mynetworklocator implements locationlistener{      private locationmanager mgr;     private string best;     location location;     public static double mylocationlatitude;     public static double mylocationlongitude;        public void starter(context context) {  //        mgr = (locationmanager) getsystemservice(location_service);         mgr = (locationmanager) context.getsystemservice(context.location_service);         criteria criteria = new criteria();         best = mgr.getbestprovider(criteria, true); //        location = mgr.getlastknownlocation(locationmanager.gps_provider);         location = mgr.getlastknownlocation(locationmanager.network_provider);         dumplocation(location);      }     public void onlocationchanged(location location) {         dumplocation(location);     }      public void onproviderdisabled(string provider) {         // todo auto-generated method stub      }      public void onproviderenabled(string provider) {         // todo auto-generated method stub      }      public void onstatuschanged(string provider, int status, bundle extras) {         // todo auto-generated method stub      }      protected void onpause() {  //        super.onpause();         mgr.removeupdates(this);     }      protected void onresume() {  //        super.onresume();         mgr.requestlocationupdates(best, 15000, 10, this);     }      private void dumplocation(location l) {          if (l != null){              mylocationlatitude = l.getlatitude();             mylocationlongitude = l.getlongitude();         }     }    } 

i have learned getsystemservice(location_service) needs context. wrote context.getsystemservice(context.location_service) , set parameter in method starter.

calling method via

mnl1.starter(this); 

doesn't work because "this" not context.

so tried

mnl1.starter(context); 

and thought give again parameter in run(context context).

but doesn't work because new timertask must start run() without parameters.


well, hope understand problem. try give context activity down getsystemservice, timertask starts run() without parameters.

is there out there can me? code snipped shows me how solve problem wonderful.

kind regards germany,


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -