WorkManager using setForegroundAsync() with ListenableWorker

78
February 14, 2021, at 03:10 AM

A very useful guide and code snippet is provided for using setForegroundAsync() for a long-running worker in the context of a Worker in WorkManager.

What is the correct approach when using a ListenableWorker rather than Worker? I am already using an approach based on the code snippet from this answer, and now want to try accessing location in the foreground as part of what is done in startSomeAsyncStuff() from that code snippet... based on this part of the above guide.

To modify my existing ListenableWorker approach, I have started with the code snippet from the above answer, and have come up with:

public ListenableFuture<Result> startWork() {
 
    String progress = "Starting Download";
    setForegroundAsync(createForegroundInfo(progress));
    return CallbackToFutureAdapter.getFuture(completer -> {
        // Your method can call set() or setException() on the
        // Completer to signal completion
        download(completer, inputUrl, outputFile);
        // This value is used only for debug purposes: it will be used 
        // in toString() of returned future or error cases.
        return "downloading";
    });
}

The download() and createForegroundInfo() functions would be as defined in the example from here, except that I would return from the latter with:

return new ForegroundInfo(0, notification, FOREGROUND_SERVICE_TYPE_LOCATION);

Is this correct, and if not, what is?

READ ALSO
Update XML with an SQL query

Update XML with an SQL query

Let's say we have the following XML file:

77
TypeError: bdcls.map is not a function

TypeError: bdcls.map is not a function

I am finding it hard to consume data from the API i hosted on heroku using Axios

62
Binding a value to input form select-element in Blazor only sets the value after making a selection

Binding a value to input form select-element in Blazor only sets the value after making a selection

I am using Blazor Server and trying to bind a value to the selected option in a HTML form

32
Docker SpringBoot MySql: The driver has not received any packets from the server

Docker SpringBoot MySql: The driver has not received any packets from the server

I'm trying to create a spring boot api and mysql database, both using docker, but I'm getting this wrong that I can't solveI tried to change the ports, but to no avail, it seems that my app container can't fill the mysql database

79