2

I have a BroadcastReceiver here:

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);
    }
}

A Notification Server:

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));

}

}

I want to start a WorkManager task onReceive of the Broadcast Receiver. The problem is I can't do this statically as I need access to the current WorkManager object. The example code that Google provides here: https://github.com/googlecodelabs/android-workmanager/blob/master/app/src/main/java/com/example/background/BlurActivity.java

Grabs the ViewModel like this: ViewModelProviders.of(this).get(BlurViewModel.class);

I can't do this obviously because my notification server class is not a view model. How should I approach this problem?

Josh
  • 386
  • 5
  • 14
  • Hi @Dylan this questions is out of topic. Is it possible to use Work Manager as Broad cast receiver. My workmanger need to trigger on every one hour (This i can do by default) . But additionally i have to trigger the same workmanager while the location gets turn on.. Is it possible without using broad cast receiver? – Suresh Sep 17 '18 at 14:28
  • I'm unsure. Just beware that implicit broadcasts will no longer work after Oreo https://developer.android.com/distribute/best-practices/develop/target-sdk#prenougat. So if your location turning on ACTION is not specific to your application and it's not whitelisted, it won't work. – Josh Sep 17 '18 at 15:38
  • Is there any way to make this things happen? – Suresh Sep 17 '18 at 15:55
  • I'm just as confused about this whole thing as you are, there's not a lot of documentation out there right now – Josh Sep 17 '18 at 15:56
  • Thanks for the Update @Dylan. If you got anything please let me know.. – Suresh Sep 17 '18 at 15:58
  • @Suresh https://stackoverflow.com/questions/5888502/how-to-detect-when-wifi-connection-has-been-established-in-android/52330503#52330503 Look at these answers for how to replace implicit broadcasts on Android 8 with Workmanager – Josh Sep 20 '18 at 16:20
  • 1
    ".setConstraints(Constraints.Builder().setRequiredNetworkType(UNMETERED).build " Is this will execute if the Network Change(Like On/Off WiFi)? I think it's precondition to execute my work manager at any network type. Like Downloading large file only if WiFi is ON.. Please correct me. – Suresh Sep 20 '18 at 19:22
  • Yes. It seems Workmanager is enqueuing this task to be completed once the user once the user is on an unmetered internet connection. Refer to this for the difference between the Networktypes: https://stackoverflow.com/questions/37217283/detect-network-state-change-using-jobschedulers-in-android – Josh Sep 20 '18 at 19:27
  • Thanks for your support. The .setRequiredNetworkType(UNMETERED) in Constrain is working. – Suresh Sep 21 '18 at 15:00

1 Answers1

2

For anyone that sees this, you can use WorkManager.getInstance() to get the WorkManager object statically. There is only one instance of WorkManager, just make sure you initialize it like this on the start of your application: WorkManager.initialize(this, new Configuration.Builder().build());

Android Custom Work Manager Config official documentation

Dhara Bhavsar
  • 345
  • 4
  • 11
Josh
  • 386
  • 5
  • 14