Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run code every day when app isn't open

Tags:

android

I want to run some code every day (every 24 hours). Problem is if user doesn't open app. How to run code when the application isn't open?

like image 455
Mate Matenson Avatar asked Aug 28 '15 16:08

Mate Matenson


People also ask

How do you run the app is when the app is closed?

you can use onAppForegroundStateChange() method which call when app is open and closed. this method is only called when your app comes in foreground/background. onAppForegroundStateChange() method is better then you used onPause() method because onPause method is also called every time when you go to other activity.

What is the current recommend way to handle long running background tasks?

Recommended solutionScheduling deferred work through WorkManager is the best way to handle tasks that don't need to run immediately but which ought to remain scheduled when the app closes or the device restarts.

How do I find my first app launch Android?

Just check for some preference with default value indicating that it's a first run. So if you get default value, do your initialization and set this preference to different value to indicate that the app is initialized already.


1 Answers

In android to run background periodic task you can use various ways and some of them are:

  1. JobScheduler (Only for API 21 or above)

Android has added this class on API 21 for documentation here is the link.

  1. JobSchedulerCompat - Backport of JobScheduler library for API 11 or above

You can find everything about library here.

  1. Use alarm manager to handle periodic task

You can also use AlarmManager to schedule periodic task. A full article to implement it is posted here.

  1. Use GCM(Google Cloud Messaging) Network Manager to schedule periodic task.

You can have a look at this docs link to implement it.

Example for periodic Task using GCM Network Manager

Add dependency in your project level build.gradle.

compile 'com.google.android.gms:play-services-gcm:7.5.0'

Create a java class that extends toGcmTaskService

public class BackgroundTaskHandler extends GcmTaskService {

    public BackgroundTaskHandler() {
    }

    @Override
    public int onRunTask(TaskParams taskParams) {
         //Your periodic code here
    }
}

Declare the service in manifest.xml

    <service
        android:name=".BackgroundTaskHandler"
        android:exported="true"
        android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
        <intent-filter>
            <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY" />
        </intent-filter>
    </service>

Now schedule the periodic task from any class as:-

    String tag = "periodic";

    GcmNetworkManager mScheduler = GcmNetworkManager.getInstance(getApplicationContext());

    long periodSecs = 60L;// 1 minute

    PeriodicTask periodic = new PeriodicTask.Builder()
            .setService(BackgroundTaskHandler.class)
            .setPeriod(periodSecs)
            .setTag(tag)
            .setPersisted(true)
            .setUpdateCurrent(true).setRequiredNetwork(com.google.android.gms.gcm.Task.NETWORK_STATE_CONNECTED)
            .build();
    mScheduler.schedule(periodic);
like image 117
Aawaz Gyawali Avatar answered Oct 03 '22 06:10

Aawaz Gyawali