Start work and do a pool every 2 minutes, the application is closed or closed from memory

I am using android-priority-job-queue 'com.github.yigit: android-priority-jobqueue'. To make sure it works 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 pauses whenever the application closes or is deleted from memory. How to make work work continuously and make a pool every 2 minutes when the application is closed or deleted from memory. Is there something like a sticky service or android.permission.BIND_JOB_SERVICE ?

Creating a server federation system. Require API 15 above.

+4
source share
3 answers

AlarmManager Intent BroadcastReceiver , .

, - , - ( ). .

+2

AlarmManager Service, . AlarmManager.

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");
            }

        }

    }

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");
    }
}

, .

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

    }


}
+2

Why do you want If I were you, I would look at SyncAdapters. You can design them to work even when you are in the background, and this is better architecturally than handling AlarmManagers. Check them out here, Sync adapters . They are better suited for battery life. Allow authentication and add the plugin architecture to the code base.

0
source

All Articles