【问题标题】:Starting WorkManager task from a BroadcastReceiver从 BroadcastReceiver 启动 WorkManager 任务
【发布时间】:2019-03-16 09:35:33
【问题描述】:

我在这里有一个广播接收器:

NotificationServiceReceiver:

public class NotificationServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(RestService.ACTION_PENDING_REMINDERS_UPDATED)) {
        //Reminders updated
        NotificationServer.startNotificationWorkRequest(context);
    }
}

通知服务器:

public class NotificationServer extends IntentService {

private static final String LOG_TAG = "NotificationService";
public static final String ACTION_SHOW_NOTIFICATION = "com.android.actions.SHOW_NOTIFICATION";
// this is a bypass used for unit testing - we don't want to trigger this service when the calendar updates during
// the intergration tests
public static boolean sIgnoreIntents = false;
private WorkManager mWorkManager;
private LiveData<List<WorkStatus>> mSavedWorkStatus;

public NotificationServer() {
    super(NotificationServer.class.getName());
    mWorkManager = WorkManager.getInstance();
}

/**
 * Handles all intents for the update services. Intents are available to display a particular notification, clear all
 * notifications, refresh the data backing the notification service and initializing our timer. The latter is safe to
 * call always, it will check the current state of on-device notifications and update its timers appropriately.
 *
 * @param intent - the intent to handle. One of ACTION_SHOW_NOTIFICATION,
 * ACTION_REFRESH_DATA or ACTION_INIT_TIMER.
 */
@Override
protected void onHandleIntent(Intent intent) {
    startNotificationWorkRequest(this);
}

public void startNotificationWorkRequest(Context context) {
    WorkContinuation continuation = mWorkManager
            .beginUniqueWork(IMAGE_MANIPULATION_WORK_NAME,
                    ExistingWorkPolicy.REPLACE,
                    OneTimeWorkRequest.from(CleanupWorker.class));

}

}

我想在广播接收器的接收上启动一个 WorkManager 任务。问题是我不能静态地执行此操作,因为我需要访问当前的 WorkManager 对象。 Google 在此处提供的示例代码:https://github.com/googlecodelabs/android-workmanager/blob/master/app/src/main/java/com/example/background/BlurActivity.java

像这样抓取 ViewModel:ViewModelProviders.of(this).get(BlurViewModel.class);

显然我不能这样做,因为我的通知服务器类不是视图模型。我应该如何解决这个问题?

【问题讨论】:

  • 嗨@Dylan 这个问题不在主题范围内。是否可以将工作管理器用作广播接收器。我的工作人员需要每隔一小时触发一次(默认情况下我可以这样做)。但另外我必须在打开位置时触发同一个工作管理器。是否可以不使用广播接收器?
  • 我不确定。请注意,在 Oreo developer.android.com/distribute/best-practices/develop/… 之后,隐式广播将不再起作用。因此,如果您启用 ACTION 的位置不是特定于您的应用程序且未列入白名单,则它将不起作用。
  • 有没有办法让这件事发生?
  • 我和你一样对这整件事感到困惑,现在没有很多文档
  • ".setConstraints(Constraints.Builder().setRequiredNetworkType(UNMETERED).build" 如果网络发生变化(比如开/关 WiFi),这会执行吗?我认为这是执行我的工作经理的前提条件在任何网络类型。比如只有在 WiFi 开启时才下载大文件。请纠正我。

标签: android android-studio android-workmanager


【解决方案1】:

对于看到此内容的任何人,您可以使用WorkManager.getInstance() 静态获取 WorkManager 对象。 WorkManager 只有一个实例,只需确保在应用程序启动时像这样初始化它:WorkManager.initialize(this, new Configuration.Builder().build());

Android Custom Work Manager Config official documentation

【讨论】:

    猜你喜欢
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多