WorkManager onStopped() 方法调用了两次
WorkManager onStopped() method called twice
我已经在我的项目中实现了 WorkManager
。在 doWork()
完成我的任务后,我调用了 stop(true);
并向 onStopped()
中的另一个 activity 发送了一个广播。一切正常,但问题是 onStopped()
被调用 两次 (我已经通过应用断点进行了测试)。结果 BroadcastReceiver
也被调用了两次。
我知道 WorkManager 处于 alpha 阶段。但我实际上不确定这是一个错误还是我做错了什么。请帮忙。代码如下:
public Result doWork() {
startGettingData();
stop(true);
return Result.SUCCESS;
}
@Override
public void onStopped(boolean cancelled) {
super.onStopped(cancelled);
Intent intent=new Intent(SERVER_SYNC_BROADCAST);
intent.putExtra(SYNC_RESULT_MESSAGE,responseCodes);
LocalBroadcastManager.getInstance(MyApplication.getContext()
.getApplicationContext())
.sendBroadcast(intent);
}
从 activity 的 onCreate()
调用一次:
WorkUtil.startSyncing(SyncWorker.class);
WorkUtil.java
public class WorkUtil {
private static WorkManager mWorkManager;
public static WorkUtil workUtil;
private WorkUtil() {
mWorkManager = WorkManager.getInstance();
}
public static WorkUtil getInstance() {
if(workUtil == null) {
workUtil = new WorkUtil();
}
return workUtil;
}
public static void startSyncing(Class workerClass) {
Constraints constraints = new Constraints.Builder().build();
OneTimeWorkRequest someWork = new OneTimeWorkRequest.Builder(workerClass)
.setConstraints(constraints)
.build();
OneTimeWorkRequest oneTimeWorkRequest = someWork;
mWorkManager.enqueue(oneTimeWorkRequest);
}
public static void cancelAllWork() {
mWorkManager.cancelAllWork();
}
}
如果我没理解错的话,你想在工作完成后发送广播。
你不应该在 onStopped
里面做,像这样把它移到 doWork
:
public Result doWork() {
startGettingData(); // this method should be synchronous
Intent intent=new Intent(SERVER_SYNC_BROADCAST);
intent.putExtra(SYNC_RESULT_MESSAGE,responseCodes);
LocalBroadcastManager.getInstance(MyApplication.getContext()
.getApplicationContext())
.sendBroadcast(intent);
return Result.SUCCESS;
}
onStopped
方法不是在工作完成时调用,而是在停止或取消时调用。
我已经在我的项目中实现了 WorkManager
。在 doWork()
完成我的任务后,我调用了 stop(true);
并向 onStopped()
中的另一个 activity 发送了一个广播。一切正常,但问题是 onStopped()
被调用 两次 (我已经通过应用断点进行了测试)。结果 BroadcastReceiver
也被调用了两次。
我知道 WorkManager 处于 alpha 阶段。但我实际上不确定这是一个错误还是我做错了什么。请帮忙。代码如下:
public Result doWork() {
startGettingData();
stop(true);
return Result.SUCCESS;
}
@Override
public void onStopped(boolean cancelled) {
super.onStopped(cancelled);
Intent intent=new Intent(SERVER_SYNC_BROADCAST);
intent.putExtra(SYNC_RESULT_MESSAGE,responseCodes);
LocalBroadcastManager.getInstance(MyApplication.getContext()
.getApplicationContext())
.sendBroadcast(intent);
}
从 activity 的 onCreate()
调用一次:
WorkUtil.startSyncing(SyncWorker.class);
WorkUtil.java
public class WorkUtil {
private static WorkManager mWorkManager;
public static WorkUtil workUtil;
private WorkUtil() {
mWorkManager = WorkManager.getInstance();
}
public static WorkUtil getInstance() {
if(workUtil == null) {
workUtil = new WorkUtil();
}
return workUtil;
}
public static void startSyncing(Class workerClass) {
Constraints constraints = new Constraints.Builder().build();
OneTimeWorkRequest someWork = new OneTimeWorkRequest.Builder(workerClass)
.setConstraints(constraints)
.build();
OneTimeWorkRequest oneTimeWorkRequest = someWork;
mWorkManager.enqueue(oneTimeWorkRequest);
}
public static void cancelAllWork() {
mWorkManager.cancelAllWork();
}
}
如果我没理解错的话,你想在工作完成后发送广播。
你不应该在 onStopped
里面做,像这样把它移到 doWork
:
public Result doWork() {
startGettingData(); // this method should be synchronous
Intent intent=new Intent(SERVER_SYNC_BROADCAST);
intent.putExtra(SYNC_RESULT_MESSAGE,responseCodes);
LocalBroadcastManager.getInstance(MyApplication.getContext()
.getApplicationContext())
.sendBroadcast(intent);
return Result.SUCCESS;
}
onStopped
方法不是在工作完成时调用,而是在停止或取消时调用。