1

I was implementing Unique Periodic Work Manager with an Interval of 15 Mins to send some data to Firebase Database but seems to be not working on Xiaomi phones.

It is working on an emulator but not working on Chinese custom ROM phones.

Attaching below my code to check if I am doing anything wrong

Inside Activity:

Constraints constraints = new Constraints.Builder()
                    .setRequiresCharging(false)
                    .build();

final PeriodicWorkRequest periodicWorkRequest1 = new PeriodicWorkRequest.Builder(UploadWorker.class, 15, TimeUnit.MINUTES).setConstraints(constraints).build();
WorkManager workManager = WorkManager.getInstance(this);
workManager.enqueueUniquePeriodicWork("SYNC_DATA", ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest1);

Inside UploadWorker.class

@Override
public Result doWork() {
     Log.i(TAG, "doWork: inside dowork()");

     String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
     FirebaseDatabase.getInstance().getReference().child("Redmi").child(currentDateTimeString).setValue(myDetails);

     // Indicate whether the work finished successfully with the Result
     return Result.success();
}

I also tried the autostart permission but still not working. Please help me. Please!!!

Shubham Agrawal
  • 1,252
  • 12
  • 29
  • Did you read https://dontkillmyapp.com/xiaomi – ianhanniballake Jun 16 '21 at 13:33
  • @ianhanniballake I did check out this website and it worked also, but I can't expect the user to lock the app in the recent apps section. There is no workaround from developer's point of view, is it? – Shubham Agrawal Jun 17 '21 at 06:54
  • Did you read the [Solution for devs section](https://dontkillmyapp.com/xiaomi#dev-solution)? "So far, no workarounds on the dev side are known." – ianhanniballake Jun 17 '21 at 15:26

1 Answers1

0

From what I can judge of the situation it is most likely that your WorkManager is being closed off and as soon as you reopen the app the scheduled task runs.

This is a known issue of WorkManager which is not really a WorkManager issue but an OS issue. The behavior for WorkManager is as follows:

Task manager close: Work continues (after a bit)

Reboot device (work running): Work continues after reboot done

App info "Force stop": Work stops, will only continue when app is started again

Reboot device (work was "Force Stopped"): Work does not continue until the app is started again

And the problem with this is some devices implement killing the app from the recents menu as a force stop. Most popular apps are whitelisted by default for the OS. There might be a workaround which can include user to whitelist your app maybe although I don't think that is a proper one. Xiaomi is very aggressive with this killing. There isn't much that can happen, but I suggest you read through the following.

You can fine more discussion here and here too for some further idea.

che10
  • 2,176
  • 2
  • 4
  • 11