1
votes

I have been using android-priority-job-queue V2 for 2 years now (great library). Android Workmanager just appeared. What I like about it is that it is really easy to setup and you don't have to fight with the JobScheduler, Firebase JobDispatcher, or AlarmManager, it is done for us. With android-priority-job-queue V2 I had to manually setup the FrameworkJobSchedulerService/GcmJobSchedulerService.

Is it possible with the Workmanagerto add a certain http url as a constraint? Sometimes, there are some data that you want to send to a server that is in a protected wifi and you are far from the geographic area. You take a picture (for example) that you have to save on that server later, when you are in the geographic area of the server and your device connects to the wifi. A url constraint will ensure that if the url is not reachable, the work request is never executed nor flushed (set as Failure). When the url constraint is met in agreement with connectivity, the work request is executed.

With android-priority-job-queue V2, I had to implement a logic that checked when android-priority-job-queue V2's shouldReRunOnThrowable(Throwable throwable, int runCount,int maxRunCount) threw a http exception and save the unsent data with Room Persistence Library. Then later, every time I detect a connectivity change status via the BroadCastReceiver, I try to resend the not sent data and flag those who where definitively sent (those who finished the onRun() in android-priority-job-queue V2). Later I would just flush every sent data from the data base via a periodic routine. This solution works. But let's say I want to send a different sort of data (picture, location, etc.), I have to rewrite the same routine for every different type of Job. I don't see how to do the same with the WorkManager.

Can someone help me, by pointing me the part of the documentation I might have missed if this feature is implemented? Or at least a way to implement it with the WorkManager.

P.S: WhisperSystems' JobManager, allowed me to add a custom url. Yet the problem was that its persistance didn't worked on some devices (some Samsung models). When I crashed the app, data were never recovered. I went on github for help, to no avail. I am not accusing anyone of anything I am just trying to show that I have been looking for a solution for this.

1

1 Answers

0
votes

I think I have found a way to make it work with the WorkManager and it works so far as intended. These are the steps to make it work:

1. I put each location information (containing its URL destination) in a OneTimeWorkRequest, with NetworkType.CONNECTED as constraint, and set a backoff strategy for the retry (lets say I set it to 10 minutes).

2. In the class extending the Worker, in the doWork() :

a. Ping the server (you can set it with your backend) surrounded by a try/catch

b. If successful, you send the location data. Upon receiving the success status for the http/https request, you return WorkerResult.SUCCESS

c. If when you ping the server it cashes and exception or when sending the location data you don't receive a http success, you return WorkerResult.RETRY

Now, unless the user resets the application, the location data is guaranteed to reach its desired server (when the server is available)

@NonNull
@Override
public WorkerResult doWork() {
    try {
         //ping server (according to its URL)
         //if the ping successful we send the location data to the server 
         //if the ping isn't successful we return WorkerResult.RETRY

        } catch (Exception ex) {
            ex.printStackTrace();
            return WorkerResult.RETRY;
        }
    }
    return WorkerResult.SUCCESS;
}