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?