App Using Asynctask Still Hogging The Ui Thread?
Solution 1:
ANSWER?... I've massively simplified the above code to try to work out what might be wrong. I created a bog standard App and used the following:
package uk.co.baroquedub.testcheck;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;
publicclassMainActivityextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doSendTask task = newdoSendTask();
task.execute(newString[] { "urlString" });
}
protectedvoid showDialog (String response){
Toast.makeText(this, response, Toast.LENGTH_SHORT).show();
finish();
}
privateclassdoSendTaskextendsAsyncTask<String, Void, String> {
@OverrideprotectedStringdoInBackground(String... urls) {
String response = "";
try {
Thread.sleep(5000);
response = "Waited";
}
catch (InterruptedException ex) { }
return response;
}
@OverrideprotectedvoidonPostExecute(String result) {
showDialog(result);
}
}
}
This has allowed me to see what's going wrong: my app is opening on top of the browser (a white screen appears with a title bar showing the name of the app) I wasn't aware of this with my proper app (above) because I was using a Theme that used a transparent background.
See: screencast for demo of problem
So although the email is being sent as an AsyncTask, while this is happening the app itself is appearing on top of the browser - which is what is stopping it from being accessible. (I'll post a request for help on this as a separate question)
Solution 2:
No, AsyncTask is not bond to your UI thread and you will not be blocking it as you did while doing some lengthy operation in your onCreate()
.
Solution 3:
I think you have a mistake in your code:
doSendTasktask=newdoSendTask();
task.execute(url); // if you want to put more urls task.execute(url,url1,url2);try {
sender.sendMail(subjectText,
url[0], // get the first url
senderEmail,
recipientEmail);
response = "Email sent";
} catch (Exception e) {
//Log.e("SendMail", e.getMessage(), e);
response = "Error sending email";
}
Solution 4:
ASyncTasks are, indeed, executed on their own thread, as written on their reference page.
When you create the intent for this activity, you pass the flag 'FLAG_ACTIVITY_CLEAR_TOP', which reads something like '...all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.'. Doesn't this mean that the old activity is locked until this new one terminates?
Post a Comment for "App Using Asynctask Still Hogging The Ui Thread?"