My work manager is not working after kill the application

Viewed 3314

My android application doesn't run the workmanager when I killed the application. The workmanager stop working when the application process killed.

my code is :

PeriodicWorkRequest workRequest =
                new PeriodicWorkRequest.Builder(UploadWorker.class, 1, TimeUnit.MINUTES)
                        .setConstraints(constraints)
                        .build();
        WorkManager.getInstance().enqueue(workRequest);

and I have also applied Jobscheduler but the program is working when application still alive in the process. but when the application closed the workmanager is not working. I have set the periodic time is 1 min.

3 Answers

The workmanager DOES NOT stop working when the application process killed, you set the period to 1 minute and minimum is 15 minute, so workmanager does the work you specified every 15 minute instead of 1 minute and ignore that 1 minute!

I got the answer thanks alot. Actually WorkManager is different thing like Jobschduler. After limitation on background services on greater version to Oreo then 3-4 methods launched for background limitation services. Those are: Firebase FCM JobSchduler WorkManger

but service is the different things here. If you want close or kill the application then you need to mention alarm manager and set time.

So you need 1 Maninactivity where you can mention your WorkManager and then you also need to implement Alarm Manager. Code for Alarm manager is :

// Alarm Manager
        int i=30;
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        assert alarmManager != null;
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ (i * 1000), pendingIntent);

now you can run the service in background. Remmeber time will be minimum 30 seconds, you can also run after every 2 second but if you have application on playstore and application target SDk is oreo or more than oreo then playstore will disable your application as per policy as per documentation. if you need more information then leave a comment, i will write more about it. I have successfully information now about JobSchduler, WorkManager.

Related