简体   繁体   中英

how to start a handler from service after 15 second to prevent app from sleep and send location to webservice?

I am working on GPS tracking Application in which I need to track location after 15 seconds. I have use handler to start service from onStartCommand() from Service class. but After 15 second or after some time after it handler does not execute always. I have also used WakeLock to prevent app from sleeping. how to overcome this issue?

In short:

new Handler().postDelayed(() -> { ... }, 15000);

The action (lambda here) will be executed after the timeout (15,000 ms.)

Expanded it might look like this:

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        //Track my location
    }
}, 15_000);

Use Timer instead of Handler as shown below

new java.util.Timer().schedule(new java.util.TimerTask() {
            @Override
            public void run() {
                //do your task here
            }
        }, 15000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM