简体   繁体   中英

Need Android universal background schedule task runner for all device manufacturers

I need to run a network request in every 12 hours from my Android app. I've used Schedule Workmanager to update the network request. It perfectly works on my Android Samsung M40 device and Emulator but when I try with some other devices like Huawei, Xiaomi or RealMe it fails to update periodically.

Here is my implementation:

WorkManager mWorkManager;
                    mWorkManager = WorkManager.getInstance(context);
                    
                    Constraints constraints = new Constraints.Builder()
                            .setRequiredNetworkType(NetworkType.CONNECTED)
                            .build();


                    PeriodicWorkRequest periodicSyncDataWork =
                            new PeriodicWorkRequest.Builder(AccessTokenUpdateWorker.class, 12, TimeUnit.HOURS)
                                    .addTag("ACCESS_TOKEN_SYNC_DATA")
                                    .setConstraints(constraints)
                                    .setInitialDelay(5, TimeUnit.MINUTES)
                                    // setting a backoff on case the work needs to retry
                                    .setBackoffCriteria(BackoffPolicy.LINEAR, PeriodicWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS)
                                    .build();
                    mWorkManager.enqueueUniquePeriodicWork(
                            "ACCESS_TOKEN_SYNC_DATA",
                            ExistingPeriodicWorkPolicy.KEEP, //Existing Periodic Work policy
                            periodicSyncDataWork //work request
                    ); 

and here is the AccessTokenUpdateWorker code:

public class AccessTokenUpdateWorker extends Worker {

    private Context context;
    private AppPreferenceHelper preferenceHelper;

    public AccessTokenUpdateWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
        this.context = context;
    }

    @NonNull
    @Override
    public Result doWork() {

        preferenceHelper = new AppPreferenceHelper(context);
        networkUpdate();
       
        return null;
    }

    private void networkUpdate() {
      ....
    }
}

This is a known issue with some Android OEM that heavily modify this part of Android for battery optimization ( dontkillmyapp.com/ ).

Aside having you application added to the OEM's allow-list, you can really only report the issue:

  • to the OEM to avoid this kind of breaking changes.
  • to Google, to add a test in the CTS and avoid these behaviors by the OEMs.

WorkManager is a library bundled with your application and has the same constraints of a normal application.
From WorkManager's issue tracker: are the Chinese manufacturers (Huawei, Oppo, Xiaomi...) supported?

You can take a look at the autostarter library that tries to simplify the process for a user to add an application to the OEM's allowlist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM