UMESH0492
UMESH0492

Reputation: 1731

Make job run and do pooling every 2 mins, whether the app is closed or removed from memory

I am using android-priority-job-queue 'com.github.yigit:android-priority-jobqueue'. To make sure it runs every 2 minutes. Below is the code

public PoolingJob () {

    // This job requires network connectivity,
    // and should be persisted in case the application exits before job is completed.
    super (new Params (PRIORITY).requireNetwork ().groupBy (Const.POOLING_QUEUE_GROUP)
            .delayInMs (120000).persist ());//120 sec delay
}

But it get suspended whenever the app is closed or removed from memory. How do make job run continuously and do pooling every 2 mins, when the app is closed or removed from memory. Is there anything like sticky service or android.permission.BIND_JOB_SERVICE ?

Building server pooling system. Require API 15 above.

Upvotes: 4

Views: 544

Answers (3)

ichthyocentaurs
ichthyocentaurs

Reputation: 2175

Why do you want to If I were you I would look into SyncAdapters instead. You can design them to work even when you are in background and it is better architecturally than handling AlarmManagers. Check them out here, Sync Adapters. They are better for the battery performance. Allow Authentication, and add a plugin architecture to the code base.

Upvotes: 0

Gopal Singh Sirvi
Gopal Singh Sirvi

Reputation: 4649

By using AlarmManager and Service you can achieve this. Read documentation here AlarmManager.

Here is code block to set and remove the alarm for repeating tasks

To set The alarm

public void createAlarm() {
        ServerDetails serverDetails = new ServerDetails(this);
        if (serverDetails.isSettingsOK()) {
            Intent i = new Intent(this, MyService.class);
            if (PendingIntent.getService(this, 0, i,
                    PendingIntent.FLAG_NO_CREATE) == null) {
                PendingIntent pendingIntent = PendingIntent.getService(this, 0,
                        i, PendingIntent.FLAG_CANCEL_CURRENT);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                int interval = 2 * 60 * 1000;
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        System.currentTimeMillis() + interval, interval,
                        pendingIntent);
                Log.d("alarm", "alarm set successfully " + interval);
            } else {
                Log.d("alarm", "alarm already set");
            }

        }

    }

To Cancel the alarm

public void cancelAlarm() {
    Intent i = new Intent(this, MyService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, i,
            PendingIntent.FLAG_NO_CREATE);
    if (pendingIntent != null) {
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
        pendingIntent.cancel();
        Log.d("alarm", "alarm cancelled");
    }
}

This is the service which will run in background when alarm activates.

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public void onCreate() {

        super.onCreate();
        Log.d("service Created", "service created");

    }


    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
       doYourTaskHere();
    }

    private void doYourTaskHere() {

        // call here webservice or any other task here which you want to do in every two minutes

    }


}

Upvotes: 2

SpaceBison
SpaceBison

Reputation: 3202

You can use AlarmManager to send Intents with a given interval and run the job in a BroadcastReceiver regardless of whether the app is running or not.

That being said, two minutes is a very short interval and you should avoid performing any task (especially network-related) that often. It will increase battery drain considerably.

Upvotes: 2

Related Questions