Asynctask.get() No Progress Bar
Solution 1:
You do not need a timer at all to do what you're attempting (for some reason I thought you were trying to loop the AsyncTask
based on your comments above which resulted in mine.). If I understand correctly you're issue is with the loss of service. You have an AsyncTask
that you start which may or may not finish depending on certain conditions. Your approach was to use get and cancle the task after a fixed time in the event that it did not finish executing before then - the assumption being if the task didn't finish within the 10 second cut off, service was lost.
A better way to approach this problem is to use a boolean flag that indcates whether network connectivity is available and then stop the task from executing if service is lost. Here is an example I took from this post (I apologize for the formatting I'm on a crappy computer with - of all things - IE8 - so I can't see what the code looks like).
publicclassMyTaskextendsAsyncTask<Void, Void, Void> {
privatevolatilebooleanrunning=true;
privatefinal ProgressDialog progressDialog;
publicMyTask(Context ctx) {
progressDialog = gimmeOne(ctx);
progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(newOnCancelListener() {
@OverridepublicvoidonCancel(DialogInterface dialog) {
// actually could set running = false; right here, but I'll// stick to contract.
cancel(true);
}
});
}
@OverrideprotectedvoidonPreExecute() {
progressDialog.show();
}
@OverrideprotectedvoidonCancelled() {
running = false;
}
@Overrideprotected Void doInBackground(Void... params) {
while (running) {
// does the hard work
}
returnnull;
}
// ...
}
This example uses a progress dialog that allows the user to cancle the task by pressing a button. You're not going to do that but rather you're going to check for network connectivty and set the running boolean based on whether your task is connected to the internet. If connection is lost - running will bet set to false which will trip the while loop and stop the task.
As for the work after the task complete. You should NEVER use get. Either (1) put everything that needs to be done after the doInBackgroundCompletes in onPostExecute (assuming its not too much) or (2) if you need to get the data back to the starting activity use an interface. You can add an interface by either adding as an argument to your tasks constructor or using a seperate method that sets the interface up. For example
publicvoidsetInterface(OnTaskComplete listener){
this.listener = listener;
}
Where OnTaskComplete listener is declared as an instance variable in your AsyncTask
. Note the approach I am describing requires using a seperate AsyncTask
class. Your's is private right now which means you need to change your project a little.
UPDATE
To check connectivity I would use something like this.
publicbooleanisNetworkOnline() {
boolean status=false;
try{
ConnectivityManagercm= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfonetInfo= cm.getNetworkInfo(0);
if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) {
status= true;
}else {
netInfo = cm.getNetworkInfo(1);
if(netInfo!=null && netInfo.getState()==NetworkInfo.State.CONNECTED)
status= true;
}
}catch(Exception e){
e.printStackTrace();
returnfalse;
}
return status;
}
You can check to see if there is an actual network connection over which your app can connect to ther server. This method doesn't have to be public and can be part of you're AsyncTask
class. Personally, I use something similar to this in a network manager class that I use to check various network statistics (one of which is can I connect to the internet).
You would check connectivity before you started executing the loop in your doInBackground method and then you could periodicly update throughout the course of that method. If netowkr is available the task will continue. If not it will stop.
Calling the AsyncTask
built in cancle method is not sufficient becuase it only prevent onPostExecute from running. It does not actually stop the code from execting.
Post a Comment for "Asynctask.get() No Progress Bar"