因此,我声明了我的Worker类NotifyWorker,并在MainActivity中调用它,代码如下:
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.util.Log;
import com.birbit.android.jobqueue.JobManager;
import com.birbit.android.jobqueue.config.Configuration;
import com.birbit.android.jobqueue.log.CustomLogger;
public class MainActivity extends AppCompatActivity {
//some code
@Override
protected void onCreate(Bundle savedInstanceState) {
if(checkForUpdate(this)){
return;
}
//some code
final OneTimeWorkRequest notificationWork = new OneTimeWorkRequest.Builder(NotifyWorker.class)
.setInitialDelay(1, TimeUnit.MINUTES)
.addTag(workTag)
.build();
//WorkManager.getInstance(getBaseContext()).beginUniqueWork(workTag, ExistingWorkPolicy.REPLACE, notificationWork);
WorkManager.getInstance(getApplication()).enqueue(notificationWork);
Log.v(TAG,"Worker executed");
//Set content
setContentView(R.layout.activity_main);
//some code
}
}
所以,我在运行应用程序时出现了以下错误:
Caused by: java.lang.IllegalStateException: WorkManager is not initialized properly. You have explicitly disabled WorkManagerInitializer in your manifest, have not manually called WorkManager#initialize at this point, and your Application does not implement Configuration.Provider.
因此,我遵循了开发人员文档,将所需的代码添加到我的清单中,但我无法在myApplication((类中添加以下方法:
class MyApplication extends Application implements Configuration.Provider {
@Override
public Configuration getWorkManagerConfiguration() {
return Configuration.Builder()
.setMinimumLoggingLevel(android.util.Log.INFO)
.build();
}
}
我的应用程序类如下所示:
public class xxxApplication extends Application implements androidx.work.Configuration.Provider {
private static xxxApplication instance;
private JobManager jobManager;
private BroadcastReceiver broadcastReceiver;
public xxxApplication(){
instance = this;
}
@Override
public void onCreate(){
super.onCreate();
//ensure jobmanager is configured
getJobManager();
broadcastReceiver=new NetworkChangeReceiver();
IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(broadcastReceiver, intentFilter);
}
@Override
public void onTerminate() {
super.onTerminate();
unregisterReceiver(broadcastReceiver);
}
private void configureJobManager(){
Configuration.Builder builder = new Configuration.Builder(this)
.customLogger(new CustomLogger() {
private static final String TAG = "JOBS";
@Override
public boolean isDebugEnabled() {
return true;
}
@Override
public void d(String text, Object... args) {
Log.d(TAG, String.format(text, args));
}
@Override
public void e(Throwable t, String text, Object... args) {
Log.e(TAG, String.format(text, args), t);
}
@Override
public void e(String text, Object... args) {
Log.e(TAG, String.format(text, args));
}
@Override
public void v(String text, Object... args) {
}
})
.minConsumerCount(1)
.maxConsumerCount(3)
.loadFactor(3)
.consumerKeepAlive(120);
//configure here if job scheduler involved
jobManager = new JobManager(builder.build());
}
public synchronized JobManager getJobManager() {
if (jobManager == null) {
configureJobManager();
}
return jobManager;
}
public static xxxApplication getInstance() {
return instance;
}
@Override
public **Configuration** getWorkManagerConfiguration() { //I get some conflict here , resolved by using androidx.work.Configuration as a return type
return Configuration.Builder() // Can't resolve the issue ?? what do i write here??
.setMinimumLoggingLevel(android.util.Log.INFO)
.build();
}
}
我已经评论了我遇到问题的部分,语法错误说"需要方法调用">。如何解决此问题??为什么它没有按预期工作?JobManager和WorkManager配置之间是否存在冲突?
更新:因此,jobmanager和workmanager的配置文件之间实际上存在冲突。后来不得不使用完整的位置定义调用。
我自己回答这个问题,以防它对以前使用过Job Scheduler的其他人有用,现在使用WorkManager添加后台进程。在为自定义初始化添加开发人员文档中指定的扩展时,如果您已经在应用程序类中使用了JobScheduler,请使用以下代码:
public class xxxApplication extends Application implements androidx.work.Configuration.Provider {
//OTHER CODE
@Override
public androidx.work.Configuration getWorkManagerConfiguration() {
return new androidx.work.Configuration.Builder()
.setMinimumLoggingLevel(android.util.Log.INFO)
.build();
}
}
但是,建议您将所有进程迁移到WorkManager,以避免进一步的冲突。