如果连接到WFI并连接到蜂窝网络,则为工作管理器

时间:2018-08-13 14:17:42

标签: android android-architecture-components android-jetpack android-workmanager

在我的应用程序中,我想将文件上传到服务器。那里有400- 500个文件,我正在使用工作管理器将文件上传到服务器上。如果用户已连接到WIFI,我想每15分钟上传一次单个文件;如果用户未连接到WIFI,但存在蜂窝数据,那么我想每6小时上传一次到服务器的单个文件。为此,我写了以下条件,是确定还是应该进行更改以获得更好的性能。

PeriodicWorkRequest.Builder wifiWorkBuilder =
                new PeriodicWorkRequest.Builder(FileUpload.class, 15,
                        TimeUnit.MINUTES)
                        .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.UNMETERED).build());

        PeriodicWorkRequest.Builder mobileDataWorkBuilder =
                new PeriodicWorkRequest.Builder(FileUpload.class, 6,
                        TimeUnit.HOURS)
                        .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.METERED).build());

        PeriodicWorkRequest wifiWork = wifiWorkBuilder.build();
        PeriodicWorkRequest mobileDataWork = mobileDataWorkBuilder.build();
        //WorkManager.getInstance().enqueue(wifiWork);
        //WorkManager.getInstance().enqueue(mobileDataWork);
        WorkManager.getInstance().enqueueUniquePeriodicWork("wifiJob", ExistingPeriodicWorkPolicy.REPLACE,wifiWork);
        WorkManager.getInstance().enqueueUniquePeriodicWork("mobileDataJob", ExistingPeriodicWorkPolicy.REPLACE,mobileDataWork);

0 个答案:

没有答案