问题描述
我正在使用WorkManager库(版本:1.0.0-alpha12).在我的应用中,我正在使用AndroidManifest中的以下xml行创建一个新流程.
I'm using the WorkManager library(version: 1.0.0-alpha12). In my app I'm creating a new process using the below xml line in AndroidManifest.
android:process=":myprocess"
这是我使用WorkManager的方式:
This is how I use the WorkManager:
public static void startCheckStatusWorker() {
WorkManager manager = WorkManager.getInstance();
String uniqueName = "check_status_worker";
PeriodicWorkRequest.Builder builder = new PeriodicWorkRequest.Builder(CheckStatusWorker.class, 1, TimeUnit.DAYS);
builder.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build());
PeriodicWorkRequest request = builder.build();
manager.enqueueUniquePeriodicWork(uniqueName, ExistingPeriodicWorkPolicy.KEEP, request);
}
但是,当我从新进程中调用此方法时,应用程序将崩溃.这是抛出的异常,
But when I call this method from the new process, then the app crashes. This is the exception thrown,
java.lang.IllegalStateException: WorkManager is not initialized properly. The most likely cause is that you disabled WorkManagerInitializer in your manifest but forgot to call WorkManager#initialize in your Application#onCreate or a ContentProvider.
at androidx.work.WorkManager.getInstance(WorkManager.java:139)
如果我调用相同的方法,则在正常的应用程序过程中它可以正常工作,并且不会像我建议的那样在我的应用程序清单中禁用WorkManagerInitializer.
If I call the same method, from the normal app process it works well and I don't disable the WorkManagerInitializer in my app manifest as it suggests.
是否可以从新流程中获取WorkManager实例?
Is there a way to get the WorkManager instance from the new process?
任何建议将不胜感激.谢谢.
Any suggestion would be appreciated.Thanks.
推荐答案
如果您确实确实需要将服务置于单独的进程中,则可以在AndroidManifest中禁用WorkManagerInitializer.像这样:
If you REALLY REALLY need the service to be in a separate process, you can disable the WorkManagerInitializer in the AndroidManifest.Goes something like this:
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
tools:node="remove"
android:exported="false" />
(有关官方文档,请参见 https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration#java )
(For the official documentation refer to https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration#java)
禁用它后,在您的Application类中(如果您没有该类,则应该创建自己的类并扩展android Application类),然后调用WorkManager.initialize().
After disabling it , in your Application class (if you don't have one you should create your own and extend the android Application class), and call WorkManager.initialize().
那为我解决了.
但是请注意,根据google问题论坛中的一些答案( https://issuetracker.google .com/issues/79993883 ),工作经理仍将使用主流程来进行工作(即使它是从第二个流程中调用的).
But please note, according to some answers in the google issues forums(https://issuetracker.google.com/issues/79993883), the workmanager will still use the main-process in order to do work (even if it's called from a second process).
我推荐什么?如果将服务放在非主进程上并没有太大的优势,则应该使服务位于主进程上,因为这样很难维护(例如,静态全局var将有两个实例,每个实例一个)过程)
What I recommend? If there is no serious advantage from having the service on a non-main-process, you should just make the service be on the main process since it would be harder to maintain (for example static global vars will have two instances, one for each process)
这篇关于无法从单独的进程启动WorkManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!