Reputation: 5092
I have an application that receives text messages with a BroadcastReceiver. It tries to upload them to a server and if it fails it has to retry.
I wonder which would be a good way to retry uploading. Should I use some kind of timer that fires an event like a BroadcastReceiver to retry sending messages every so often (how?) or something else?
Upvotes: 0
Views: 858
Reputation: 432
you can try to use JobScheduler to schedule your upload task.
if the task fails, just calls jobFinished(mJobParam, needsReschedule),your task will be rescheduled
if your want to set the retry policy or the retry interval to wait when the task fails,you can set setBackoffCriteria(long initialBackoffMillis, int backoffPolicy)
i hope this can help you.
Upvotes: 0
Reputation: 11497
You could use the Android AlarmManager system. You can schedule a service to keep trying to upload your data in case you get a timeout, like d.i.v.a said, or any other IO problem in the first try.
Please refer to: https://developer.android.com/training/scheduling/alarms.html to learn a bit more about alarms.
Another useful link:
If you don't want to use the system alarm for some reason, you can also try to use Thread.sleep() inside a "retry loop", assuming you're using a service, AsyncTask, etc. (Don't call Thread.sleep in the main thread!)
Upvotes: 1
Reputation: 11194
It's worth if u get the timeout and hit the upload again if extends this time out..
DefaultHttpClient h;
// ...
Log.d(TAG, "http.socket.timeout: " +
h.getParams().getParameter("http.socket.timeout"));
Log.d(TAG, "http.connection.timeout: "
+ h.getParams().getParameter("http.connection.timeout"));
Upvotes: 0