android - Periodic update of location details to server -
i new user stackoverflow , first question...
question is...
how periodic updates of location details? have tried service class , run thread inside but,this called once.
can 1 suggest me doing wrong? thank you. here code
public class myservice extends service { string gps_filter = ""; thread triggerservice; locationmanager lm; gpslistener gpslocationlistener; boolean isrunning = true; @override public void oncreate() { // todo auto-generated method stub super.oncreate(); toast.maketext(getapplicationcontext(), "hello",toast.length_long).show(); gps_filter = "mygpslocation"; } @override public void onstart(intent intent, int startid) { // todo auto-generated method stub super.onstart(intent, startid); triggerservice = new thread(new runnable(){ public void run(){ try{ looper.prepare();//initialize current thread looper. lm = (locationmanager)getsystemservice(context.location_service); toast.maketext(getapplicationcontext(), "hello1",toast.length_long).show(); gpslocationlistener = new gpslistener(); long mintime = 30000; // 5 sec... float mindistance = 10; lm.requestlocationupdates(locationmanager.gps_provider, mintime, mindistance, gpslocationlistener); looper.loop(); }catch(exception ex){ system.out.println("exception in triggerservice thread -- "+ex); } } }, "mylocationthread"); triggerservice.start(); } @override public void ondestroy() { // todo auto-generated method stub super.ondestroy(); removegpslistener(); } @override public ibinder onbind(intent intent) { // todo auto-generated method stub return null; } private void removegpslistener(){ try{ lm.removeupdates(gpslocationlistener); } catch(exception ex){ system.out.println("exception in gpsservice --- "+ex); } } class gpslistener implements locationlistener{ public void onlocationchanged(location location) { // todo auto-generated method stub double latitude = location.getlatitude(); double longitude = location.getlongitude(); sendbroadcast(filterres); postdata(); }** 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 } } } my updated answer call service class
calendar cur_cal = calendar.getinstance(); cur_cal.settimeinmillis(system.currenttimemillis()); intent intent = new intent(this, myservice.class); pendingintent pintent = pendingintent.getservice(login.this, 0, intent, 0); alarmmanager alarm = (alarmmanager)getsystemservice(context.alarm_service); public class myservice extends service { string gps_filter = ""; thread triggerservice; locationlistener locationlistener; locationmanager lm; private static final long minimum_distance_change_for_updates = 1000; // in meters private static final long minimum_time_between_updates = 1000*60; // in milliseconds protected locationmanager locationmanager; boolean isrunning = true; @override public void oncreate() { // todo auto-generated method stub super.oncreate(); gps_filter = "mygpslocation"; // locationmanager = (locationmanager) getsystemservice(context.location_service); // locationmanager.requestlocationupdates( // locationmanager.gps_provider, // minimum_time_between_updates, // minimum_distance_change_for_updates, // new mylocationlistener()); } @override public void onstart(intent intent, int startid) { // todo auto-generated method stub super.onstart(intent, startid); turngpson(); toast.maketext(getapplicationcontext(), "hello1",toast.length_long).show(); locationmanager locationmanager = (locationmanager)getsystemservice(context.location_service); locationlistener = new mylocationlistener(); locationmanager.requestlocationupdates(locationmanager.gps_provider,minimum_time_between_updates, 1.0f, locationlistener); } @override public void ondestroy() { // todo auto-generated method stub super.ondestroy(); // removegpslistener(); } @override public ibinder onbind(intent intent) { // todo auto-generated method stub return null; } // private void removegpslistener(){ // try{ // lm.removeupdates(locationmanager); // } // catch(exception ex){ // system.out.println("exception in gpsservice --- "+ex); // } // } private class mylocationlistener implements locationlistener { public void onlocationchanged(location location) { postdata(location.getlatitude(),location.getlongitude()); string message = string.format( "new location \n longitude: %1$s \n latitude: %2$s", location.getlongitude(), location.getlatitude() ); toast.maketext(myservice.this, message, toast.length_long).show(); turngpsonoff(); } public void onstatuschanged(string s, int i, bundle b) { // toast.maketext(myservice.this, "provider status changed", // toast.length_long).show(); } public void onproviderdisabled(string s) { // toast.maketext(myservice.this, // "provider disabled user. gps turned off", // toast.length_long).show(); } public void onproviderenabled(string s) { // toast.maketext(myservice.this, // "provider enabled user. gps turned on", // toast.length_long).show(); } } alarm.setrepeating(alarmmanager.rtc_wakeup, cur_cal.gettimeinmillis(), 60*1000, pintent);
try puuting things want inside run of class, thread runs every 5 seconds,
you can modify want.
public class periodicchecker extends thread { @override public void run() { while(true) { system.out.println("thread doing something"); //put code here thread.sleep(5000); } } } public otherclass { public static void main(string args[]) { thread t = new periodicchecker(); t.start(); } }
Comments
Post a Comment