Reputation: 1420
hi this is a very simple question but a complicated answer I suspect:
It's called the "mineshaft scenario"
Let me explain : imagine a simple scenario where you had to make a app that can httppost to a webpage even down a mine shaft with no signal.
i.e. How do I try/queue a submit event (HTTPPOST) if there is no connection and then poll the queue periodically (test for presence of connection) until the event can fire.
PS: be warned: please this is about "how to" not a dialogue as to the "wrongness" of this, re: synchronicity or duplicate records or overwriting data from 2 users etc etc.
Pseudo Code:
try{
MyHttpFileUploader myupload = new MyHttpFileUploader();
myupload.Start();
}
catch (InternetDownException ex){ //<-- how do I "throw" this in the start method gracefully?
GlobalQueue.Add(myupload); //<-- how do i set a timer properly that can action this queue (.Start() method) and post messages "when complete" to toast on the main ui thread but otherwise not block the ui whatsoever
}
Upvotes: 3
Views: 2842
Reputation: 2244
One approach is to use IntentService and the AlarmManager to trigger trying to send the data.
There is more information here about using Intents here and the Intent could be triggered by the AlarmManager
There are also some code samples on this thread AlarmManager not working
Upvotes: 0
Reputation: 2875
Here is a rough (because I now mainly code in c# and it wouldn't be pertinent to the question) semi psuedo-code with sun documentation snippets on the main point (thread)
Create a class that has access to whatever variables are required in your async post method that extends Thread i.e.
class WorkerThread extends Thread {
int someDataYouNeed;
MyHttpFileUploader maybeAClassYouNeedToCommunicateWithAtEnd;
boolean hasNotFinishedTask = true;
WorkerThread(int someDataYouNeed, MyHttpFileUploader callBackClass) {
this.someDataYouNeed = someDataYouNeed;
this.maybeAClassYouNeedToCommunicateWithAtEnd = callBackClass;
}
public void run() {
while(hasNotFinishedTask){
//do your work in here
//Try contact network endpoint
try{
//do a network call and if it doesnt except
hasNotFinishedWork = false
//now callback to the class firing a method maybe (I just made one up)
maybeAClassYouNeedToCommunicateWithAtEnd.Close();
}catch(TheException ex){ //do nothing or log }
if(hasNotFinishedTask){
Thread.Sleep(60000);//retry every minute
}
}
}
}
//instantiate the thread when your asynch task fails....
WorkerThread worker = new WorkerThread(42, this);
worker.start();
Now as I said - I assume your application is running on a smartphone and can be shut down... if that is the case and as long as the os allows it you may way to save state (when you create the WorkerThread) as i.e. xml in the filesystem and on the WorkerThread instantiation check if the file exists and start from where it left when it was shutdown.
On point a) Even though my example was a mail server they are both networking endpoints that can be unavailable
UPDATE:
Now I assumed that it would be straight java (and remember I'm c# so not on the platform that often) but it appears as if there is easy examples of Async generic types for this stuff in newer version of java or for android...
see this blog post for android specific multi-threading or doa google search for Android multi-threading for platform specific examples
Upvotes: 2