简体   繁体   中英

Android - Run a function at specific time

I have a application that needs to run for long hours in the background. So i'm using foreground service for that. Every 15 min, I need to trigger a function and do some process. For triggering this function I used all the possible ways like Alarm manager, Work Manager, Handler But none of them are working when app goes to background. Question : What is the best way to trigger a function at specific time, which ensures it will definitely trigger in all the possible scenarios like app in background, foreground and doze.

While most phones out there have their own version of ROM and Work manager completely depends upon the manufacturer. i Quote

The work manager depends on the device manufacturer. In my case, it is an miui device, which does not allow work manager to work in case the app is killed or rebooted. The work manager worked when I provided the application with "autostart permission".

So autostart is a help if you would use that. Now the problem with manufacturers ROM is so varying with Android users , you cannot just depend on one method to have it working on all devices. Bottomline is this

If you want to be on a safer side , and want to run the critical code without failing, i would suggest you go with a foreground service , write an implementation of timely based triggers and it will run. Basic approach is to use a TimerTask or a runnable and post delay it for 15 minutes.

Remember it must be a foreground service , and it comes with a case condition to have a foreground notification running all the time. Now that will be visible to the user and annoy them.

As a security feature of the Android platform, you cannot, under any circumstance, have a foregrounded service without also having a notification. This is because a foregrounded service consumes a heavier amount of resources and is subject to different scheduling constraints (ie, it doesn't get killed as quickly) than background services, and the user needs to know what's possibly eating their battery. So, don't do this.

However, it is possible to have a "fake" notification, ie, you can make a transparent notification icon (iirc). This is extremely disingenuous to your users, and you have no reason to do it, other than killing their battery and thus creating malware.

And if are thinking of hiding notification and it works under android 7.1 it MAY VIOLATE Google Play's developer policies.

For background service use this

//The task which you want to execute
private static class MyTimeTask extends TimerTask
{

    public void run()
    {
        //write your code here
    }
}

public static void main(String[] args) {

    //the Date and time at which you want to execute
    DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = dateFormatter .parse("2012-07-06 13:05:45");

    //Now create the time and schedule it
    Timer timer = new Timer();

    //Use this if you want to execute it once
    timer.schedule(new MyTimeTask(), date);

    //Use this if you want to execute it repeatedly
    //int period = 10000;//10secs
    //timer.schedule(new MyTimeTask(), date, period );
}

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