gpt4 book ai didi

android - WorkManager 作业未重新安排或一段时间后未触发

转载 作者:行者123 更新时间:2023-12-05 00:05:15 38 4
gpt4 key购买 nike

我在重新安排作业时遇到了 WorkManager 的问题。目前,我发现在使用 Okhttp 发起请求并在 AuthInterceptor 上引发错误后的某个时候,它会卡住并且不会启动其他作业。

这是管理工作计划第一步的 JobOrganizer 类。它链接第一个作业队列。您会看到更多未粘贴到此处的作业,但主要区别在于第一个链接作业没有 WiFi 网络限制,而其他作业则有。

object JobOrganizer {

const val WORK_INTERVAL: Long = 20
const val SCH_DATA_UPDATE_WORK_RESCHEDULE = "scheduled_data_update_work_reschedule"
const val SCH_DATA_UPDATE_WORK = "scheduled_data_update_work"

private val schDataUpdateJob: OneTimeWorkRequest
get() = OneTimeWorkRequestBuilder<SCHDataUpdateJob>()
.addTag(SCH_DATA_UPDATE_WORK)
.setConstraints(wifiConstraint)
.build()

val wifiConstraint: Constraints
get() = Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresDeviceIdle(false)
.setRequiresBatteryNotLow(false)
.setRequiresCharging(false)
.setRequiresStorageNotLow(false)
.build()

fun getWorkInfos(context: Context, tag: String): LiveData<List<WorkInfo>> {
val workManager = WorkManager.getInstance(context)
return workManager.getWorkInfosByTagLiveData(tag)
}

private fun clearWorks(workManager: WorkManager) {
workManager.pruneWork()
}

private fun cancelSCHJobs(context: Context) {
val workManager = WorkManager.getInstance(context)
workManager.cancelAllWorkByTag(SCH_DATA_UPDATE_WORK )
clearWorks(workManager)
}

fun scheduleJobs(context: Context) {
cancelSCHJobs(context)
WorkManager.getInstance(context)
.beginWith(schTypesDownloadJob)
.then(schDownloadJob)
.then(schDataUpdateJob)
.then(schDataUploadJob)
.then(schCleanupJob)
.enqueue()
FirebaseAnalytics.getInstance(context).logEvent(AnalyticsEvents.Sync.SYNC_SCH_CONFIGURE_FORM_CLEANUP, Bundle())
}
}

AuthInterceptor 类

class AuthInterceptor(private val context: Context?) : Interceptor {

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {

val originalRequest = chain.request()

if (context == null) {
return chain.proceed(originalRequest)
}

val auth = AuthRepository(context).getAuth()
if (auth.isNullOrEmpty()) {
return chain.proceed(originalRequest)
}

val version = String.format(
"%s: %s (build %s)",
BuildConfig.FLAVOR,
BuildConfig.VERSION_NAME,
BuildConfig.VERSION_CODE
)

val compressedRequest = originalRequest.newBuilder()
.header("Authorization", String.format("Bearer %s", auth[0].token))
.header("mobile-app-version", version)
.build()
return chain.proceed(compressedRequest)
}

}

重新安排自身延迟 30 分钟的更新作业。主要的 try/catch 是针对 AuthInterceptor 错误。

class SCHDataUpdateJob(var context : Context, params : WorkerParameters) : Worker(context, params) {

override fun doWork(): Result {
FirebaseAnalytics.getInstance(context).logEvent(AnalyticsEvents.Sync.SYNC_SCH_UPDATE_START, Bundle())
var success = UPDElementTypesJob(context).doWork()
if (!success) {
FirebaseAnalytics.getInstance(context).logEvent(AnalyticsEvents.Sync.SYNC_UPD_ELEMENTTYPES_ERROR, Bundle())
Log.e("SYNC", "SCHDataUpdateJob UPDElementTypesJob error")
}
FirebaseAnalytics.getInstance(context).logEvent(AnalyticsEvents.Sync.SYNC_SCH_UPDATE_FINISH, Bundle())

val dataUpdateWorkRequest = OneTimeWorkRequestBuilder<SCHDataUpdateJob>()
.setInitialDelay(JobOrganizer.WORK_INTERVAL, TimeUnit.MINUTES)
.addTag(JobOrganizer.SCH_DATA_UPDATE_WORK)
.setConstraints(JobOrganizer.wifiConstraint)
.build()

WorkManager.getInstance(applicationContext)
.enqueue(dataUpdateWorkRequest)

Log.e("SYNC", "SCHDataUpdateJob finished")
return Result.success()
}
}

这是调用 scheduleJobs() 的 fragment 。

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
super.onCreateView(inflater, container, savedInstanceState)

mainView = inflater.inflate(R.layout.fragment_draft_list, container, false)

sync = mainView!!.findViewById(R.id.sync)

sync.onClick {
mFirebaseAnalytics!!.logEvent(AnalyticsEvents.UPDATE_BUTTON_CLICKED, Bundle())
if (!ConnectionUtils.isConnectedToWifi(activity!!.applicationContext)) {
showConnectivityDialog()
} else {
sync.visibility = View.GONE
doAsync {
JobOrganizer.scheduleJobs(context!!)
}
}
}

if (forceDownload) {
JobOrganizer.scheduleJobs(context!!)
}

return mainView!!
}

在某些时候,最后一项工作没有得到安排或没有运行。有什么线索吗?

谢谢。

最佳答案

在这里您可以学习如何使用工作管理器。

新建一个项目,在app/buid.gradle文件中添加WorkManager依赖

implementation "android.arch.work:work-runtime:1.0.0"

创建 Worker 的基类:-

package com.wave.workmanagerexample;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
/**
* Created on : Mar 26, 2019
* Author : AndroidWave
*/
public class NotificationWorker extends Worker {
private static final String WORK_RESULT = "work_result";
public NotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
Data taskData = getInputData();
String taskDataString = taskData.getString(MainActivity.MESSAGE_STATUS);
showNotification("WorkManager", taskDataString != null ? taskDataString : "Message has been Sent");
Data outputData = new Data.Builder().putString(WORK_RESULT, "Jobs Finished").build();
return Result.success(outputData);
}
private void showNotification(String task, String desc) {
NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "task_channel";
String channelName = "task_name";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new
NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channelId)
.setContentTitle(task)
.setContentText(desc)
.setSmallIcon(R.mipmap.ic_launcher);
manager.notify(1, builder.build());
}
}

创建工作请求:-

让我们转到 MainActivity 并创建一个 WorkRequest 来执行我们刚刚创建的工作。现在首先我们将创建 WorkManager。该工作管理器将排队和管理我们的工作请求。

 WorkManager mWorkManager = WorkManager.getInstance();

现在我们将创建 OneTimeWorkRequest,因为我想创建一个只执行一次的任务。

OneTimeWorkRequest mRequest = new OneTimeWorkRequest.Builder(NotificationWorker.class).build();

我们使用这段代码构建了工作请求,它只会执行一次

使用 WorkManager 排队请求:-

btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWorkManager.enqueue(mRequest);
}
});

获取特定任务状态:-

mWorkManager.getWorkInfoByIdLiveData(mRequest.getId()).observe(this, new Observer<WorkInfo>() {
@Override
public void onChanged(@Nullable WorkInfo workInfo) {
if (workInfo != null) {
WorkInfo.State state = workInfo.getState();
tvStatus.append(state.toString() + "\n");
}
}
});

最后,MainActivity 看起来像这样。

package com.wave.workmanagerexample;
import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
public class MainActivity extends AppCompatActivity {
public static final String MESSAGE_STATUS = "message_status";
TextView tvStatus;
Button btnSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvStatus = findViewById(R.id.tvStatus);
btnSend = findViewById(R.id.btnSend);
final WorkManager mWorkManager = WorkManager.getInstance();
final OneTimeWorkRequest mRequest = new OneTimeWorkRequest.Builder(NotificationWorker.class).build();
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWorkManager.enqueue(mRequest);
}
});
mWorkManager.getWorkInfoByIdLiveData(mRequest.getId()).observe(this, new Observer<WorkInfo>() {
@Override
public void onChanged(@Nullable WorkInfo workInfo) {
if (workInfo != null) {
WorkInfo.State state = workInfo.getState();
tvStatus.append(state.toString() + "\n");
}
}
});
}
}

关于android - WorkManager 作业未重新安排或一段时间后未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60422333/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com