Android service periodically performing tasks in background?

11,060

You need to add а TimerTask, see how to do that here

Share:
11,060
ammar26
Author by

ammar26

Developer and Technology Geek/Freak who believe in Changing Lives.

Updated on July 20, 2022

Comments

  • ammar26
    ammar26 almost 2 years

    I have created an Android Service which I start simply in my activity

    Intent i = new Intent();
    i.setClassName("com.abc.app", MyService.class.getName());
    startService(i);
    bindService(i, connection, Context.BIND_AUTO_CREATE);
    

    Then I use onServiceConnected callback to get my service instance, so I can call its functions

    public void onServiceConnected(ComponentName name, IBinder boundService) {
                myService = IMyInterface.Stub.asInterface((IBinder) boundService);
            }
    

    I can successfully call the functions and can get the results. But I want my service to perform a task periodically in background (like a function which will be called after every few minutes) I can't call function from my activity because I finish() my activity after starting the service and I want my service to simply run in background and perform a task.