import java.util.date; import java.util.timer; import java.util.timertask; import android.app.service; import android.content.context; import android.content.intent; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.os.handler; import android.os.ibinder; import android.support.v4.content.localbroadcastmanager; import android.util.log; import android.widget.toast; public class locationservice extends service { private locationdatabasehelper mlocationdatabasehelper; private locationmodel mlocationmodel; private date mdate; private handler mhandler = new handler(); private timer mtimer = null; private int mcount = 0; public static final long notify_interval = 30 * 1000; @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { // cancel if existed if (mtimer != null) { mtimer.cancel(); } else { // recreate new mtimer = new timer(); } mlocationmodel = locationmodel.getinstance(); // schedule task mtimer.scheduleatfixedrate(new timedisplaytimertask(), 0, notify_interval); } @override public void ondestroy() { mtimer.cancel(); } private class timedisplaytimertask extends timertask implements locationlistener { @override public void run() { mhandler.post(new runnable() { @override public void run() { //i send message draw map here sendmessage(); locationmanager locationmanager = (locationmanager) getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, timedisplaytimertask.this); } }); } @override public void onlocationchanged(location location) { // location , work here } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } } private void sendmessage() { intent intent = new intent("my-event"); intent.putextra("message", "data"); localbroadcastmanager.getinstance(this).sendbroadcast(intent); } } what want user location after every 30 seconds code not work expected. gets location fast (i think every second).
i tried location way because can current location after start app.i have tried getlastknowlocation before, give me last known location far am.
please show me how fix this.thank you!
in requestlocationupdates method second parameter minimum time interval between location updates, in milliseconds, need this:
locationmanager locationmanager = (locationmanager) getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 30 * 1000, 0, timedisplaytimertask.this);
Comments
Post a Comment