简体   繁体   English

需要所有设备制造商的Android通用后台调度任务运行器

[英]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.我需要每 12 小时从我的 Android 应用程序运行一次网络请求。 I've used Schedule Workmanager to update the network request.我已经使用 Schedule Workmanager 来更新网络请求。 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.它在我的 Android 三星 M40 设备和模拟器上完美运行,但是当我尝试使用其他一些设备(如华为、小米或 RealMe)时,它无法定期更新。

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:这是 AccessTokenUpdateWorker 代码:

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/ ).这是一些 Android OEM 的已知问题,它们大量修改 Android 的这一部分以进行电池优化 ( dontkillmyapp.com/ )。

Aside having you application added to the OEM's allow-list, you can really only report the issue:除了将您的应用程序添加到 OEM 的允许列表之外,您实际上只能报告问题:

  • to the OEM to avoid this kind of breaking changes.给 OEM 以避免这种破坏性的变化。
  • to Google, to add a test in the CTS and avoid these behaviors by the OEMs.到 Google,在CTS中添加测试并避免 OEM 的这些行为。

WorkManager is a library bundled with your application and has the same constraints of a normal application. WorkManager 是与您的应用程序捆绑在一起的库,具有与普通应用程序相同的约束。
From WorkManager's issue tracker: are the Chinese manufacturers (Huawei, Oppo, Xiaomi...) supported?来自 WorkManager 的问题跟踪器:是否支持中国制造商(华为、Oppo、小米……)?

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.您可以查看自动启动程序库,该库试图简化用户将应用程序添加到 OEM 许可列表的过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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