I am using AsyncTask to perform some background calculations but I am unable to find a correct way to handle exceptions. Currently I am using the following code:
private class MyTask extends AsyncTask<String, Void, String>
{
private int e = 0;
#Override
protected String doInBackground(String... params)
{
try
{
URL url = new URL("http://www.example.com/");
}
catch (MalformedURLException e)
{
e = 1;
}
// Other code here...
return null;
}
#Override
protected void onPostExecute(String result)
{
if (e == 1)
Log.i("Some Tag", "An error occurred.");
// Perform post processing here...
}
}
I believe that the variable e maye be written/accessed by both the main and worker thread. As I know that onPostExecute() will only be run after doInBackround() has finished, can I omit any synchronization?
Is this bad code? Is there an agreed or correct way to handle exceptions in an AsyncTask?
I've been doing that in my apps, I guess there is not a better way.
You can also read Mark Murphy answer about it.
I think your code would to the job but there is already some kind of error handling built into the AsyncTask class.
You can avoid to use an extra variable by using the cancel() method and its handler method onCancelled(). When you call cancel within the doInBackground() method the onCancelled method in the UI thread. Whether you call cancel(true) or cancel(false) depends on your needs.
private class MyTask extends AsyncTask<String, Void, String>
{
#Override
protected NewsItem doInBackground(String... params)
{
try
{
URL url = new URL("http://www.example.com/");
}
catch (MalformedURLException e)
{
cancel(false/true);
}
// Other code here...
return null;
}
#Override
protected void onPostExecute(String result)
{
// Perform successful post processing here...
}
#Override
protected void onCancelled() {
super.onCancelled();
// Perform error post processing here...
}
}
This is guaranteed to work, even on SMP architecture. All the synchronization is done for you. It would however be better to use the return value to do this.
Related
I am currently working on an android application that has to handle a network connection using several AsyncTasks.
This is the first task that is establishing the connection and calling a new task which is handling the microphone input.
private class establishConnectionTask extends
AsyncTask<String, Integer, String> {
#Override
protected String doInBackground(String... params) {
try {
// initialize connection
initConnection();
MicrophoneTask micTask = new MicrophoneTask();
micTask.execute("");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
mReadInputTask = new readInputTask();
mReadInputTask.execute("");
super.onPostExecute(result);
}
Everything works fine, the connection is working and I can transfer data. Also the MicrophoneTask is doing it's job.
Here comes the problem:
In the onPostExecute method I am creating a new AsyncTask which should handle all the network input.
This is how the readInputTask looks like:
private class readInputTask extends AsyncTask<String, Integer, String>
{
#Override
protected void onPreExecute()
{
Log.d("DEBUG", "pre");
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
// blocking readInput method
Log.d("DEBUG", "doInBackground");
}
#Override
protected void onPostExecute(String result) {
Log.d("DEBUG", "post");
super.onPostExecute(result);
}
}
The readInputTask somehow gets stuck in the onPreExecute method of the readInputTask. The only output I get is "pre", eventhough I also expect "doInBackground" and "post".
Does anyone see an error or knows a solution for this?
Any help is appreciated!
mReadInputTask.execute("");
When you use AsyncTask#execute(params), the AsyncTasks are executed serially: one after the other. To execute AsyncTasks in parallel, use AsyncTask#executeOnExecutor(...).
From the docs on executeOnExecutor (Executor exec, Params... params):
This method is typically used with THREAD_POOL_EXECUTOR to allow
multiple tasks to run in parallel on a pool of threads managed by
AsyncTask, however you can also use your own Executor for custom
behavior.
I have two asyc task both perform separate network operation.I want one async task to wait for other task to finish for a single variable..I thought of doing it like perform other asyc operation onPostexecute of first one but for a single variable i have to make other task to wait first one to finish...is there any to achieve efficently
Referring to this, you can not use .execute() so;
First you have to start your tasks like this:
// Start first task
new Task1().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "");
// Start second task
new Task2().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "");
and then you can make a static variable so the both of tasks can access to it:
public static boolean task1Finished = false;
Then simple example of the tasks:
First task
private class Task1 extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
Log.d("myApp", "Task1 started");
for(int x = 0; x < 10; ++x)
{
try
{
Thread.sleep(1000);
//Log.d("myApp", "sleeped 1000 ms");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
return "";
}
#Override
protected void onPreExecute() {
}
#Override
protected void onPostExecute(String result) {
// Lets the second task to know that first has finished
task1Finished = true;
}
}
Second task
private class Task2 extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
Log.d("myApp", "Task2 started");
while( task1Finished == false )
{
try
{
Log.d("myApp", "Waiting for Task1");
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
Log.d("myApp", "Task1 finished");
// Do what ever you like
// ...
return "";
}
#Override
protected void onPreExecute() {
}
#Override
protected void onPostExecute(String result) {
Log.d("myApp", "All done here (Task2)");
}
}
Maybe asynctask is not the best tool? There are some interesting classes in the android api that can help doing specifically the synchronizing job :
Quote from android developper : "Four classes aid common special-purpose synchronization idioms.
Semaphore is a classic concurrency tool.
CountDownLatch is a very simple yet very common utility for blocking until a given number of signals, events, or conditions hold.
A CyclicBarrier is a resettable multiway synchronization point useful in some styles of parallel programming.
An Exchanger allows two threads to exchange objects at a rendezvous point, and is useful in several pipeline designs."
So I suggest looking into :
Cyclic Barrier
http://developer.android.com/reference/java/util/concurrent/CyclicBarrier.html
Exchanger
http://developer.android.com/reference/java/util/concurrent/Exchanger.html
You need to create another async in OnpostExecute of first one if you need to call the other synchronously.
The goal:
Using Google App Engine server and Android client, I'm trying to put on the Google map at the Android client Users overlays. Every 30 seconds I'm polling the server and getting Vector that contains users and adding it to the map.
Current status:
I'm dong all that using in one new thread, So after running the app I got:
weird behaviors(delayed overlays, multiple overlays) and after that crushed with ConcurrentModificationException.
After reading a bit i figured out that I need to work with AsyncTask.
Correct me if I'm wrong,But I understand that everything done in the Activity at at onCreate is "running" in UIhread so I need to put the "Logic" (All the Network handling) in doInBackground and all the UI Handling like putting overlays on the map in onPostExecute.
My Question are:
1) In the current status I'm doing:
new Thread()
{
#Override
public void run()
{
super.run();
while(true)
{
SystemClock.sleep(30000);
Vector responseFromServer = getUsersVectorFromServer();
putNewOnlineUserOnTheMap();
}
}
}.start();
What is the right way to convert this To AsyncTask?
Do I poll the server still using new thread in the doInBackground or there is right way to do this?
2) Is there a specific list of what counts as UI to put in onPostExecute or any concepts list?
In my case I guess that in need to put putNewOnlineUserOnTheMap() in onPostExecute.
Thanks.
Something similar to the following:
class UpdateTask extends AsyncTask<Void, Vector, Void>{
#Override
protected Void doInBackground(Void... params) {
// this is running in a background thread.
while (!isCancelled()) {
SystemClock.sleep(30000);
Vector responseFromServer = getUsersVectorFromServer();
// send the result back to the UI thread
// onProgressUpdate will be called then
publishProgress(responseFromServer);
}
return null;
}
#Override
protected void onProgressUpdate(Vector... values) {
// this is executed on the UI thread where we can safely touch UI stuff
putNewOnlineUserOnTheMap(values[0]);
}
}
You can't use the result of the task since the task is finished then. But you can use the progress publishing mechanism to get periodic results. If you use it like that and do the modification on the UI thread you should not get ConcurrentModificationException because you do the modifications on the one thread that can safely modify the UI.
One thing to note here: create new instances of your Vector in the background thread and then use it to update the UI. But don't touch the same object afterwards in the backgroundthread. That way you don't need any synchronization since after the background thread sends it away it is only the UI thread that touches it. (and you could use a simple ArrayList instead of a Vector)
AsyncTask uses generics and varargs.The parameters that are passed to the asyntask are . TypeOfVariableArgumentsParameters is passed into the doInBackground(), ProgressParam is used for progress information and ResultParam must be returned from doInBackground() and is passed to onPostExecute() as parameter.
example:--
protected class ParsingTask extends AsyncTask> {
private ProgressDialog loadingDialog = new ProgressDialog(JsonParserActivity.this);
protected void onPreExecute() {
loadingDialog.setMessage("loading app store..");
loadingDialog.show();
}
#Override
protected ArrayList<Items> doInBackground( Context... params ) {
// do ur process here.
return result;
}
if (!this.isCancelled()) {
}
return result;
}
#Override
protected void onProgressUpdate(String... s) {
super.onProgressUpdate(s);
Toast.makeText(getApplicationContext(), s[0], Toast.LENGTH_SHORT).show();
}
#Override
protected void onPostExecute( ArrayList<Items> response ) {
//if u r dealing with list view and adapters set the adapter here at the onPostExecute()
loadingDialog.dismiss();
}
#Override
protected void onCancelled() {
super.onCancelled();
Toast.makeText(getApplicationContext(), "The operation was cancelled", 1).show();
}
}
You can use AsyncTask like below. Hope this will help you..
Class YourClass{
void YourClass(){
NetworkTask nT = new NetworkTasK();
nT.execute();
}
}
protected class NetworkTask extends AsyncTask<Void, String, Boolean>
{
#Override
protected Boolean doInBackground(Void... params)
{
try
{
String response;
while(keepreceiving)
{
response = in.readLine();//Prog Counter stops here until getting i/p.
if(response != null)
yourFunctionForResponse(response);
}
}
catch (Exception ex)
{
}
return null;
}
private void yourFunctionForResponse(String response){
//things to do....
}
}
You may also try runOnUiThread(Runnable action) along with this to implement your work.
I am new to programming and I have struggled with this for days and cannot get it to work.
I have a loop to execute some tasks. It will display a picture in an ImageView for that particular task. So it goes something like this:
setImageResource
do some task
wait for 1 second
then start everything again until the commands end
when all commands end, close that activity by calling finish()
I tried to use a handler with runnable and asynctask but I cannot get it to work. The UI is always updated later than the 1 second delay. As I call finish() after the commands ends, the photo seems not show up at all.
for(int i=0; i<actions.size(); i++) {
switch((int)actions.get(i)[0]) {
case 0:
ivActionIcon.setImageResource(R.drawable.image);
sleeping=true;
DelayAsyncTask delay=new DelayAsyncTask();
delay.execute();
while(sleeping){};
break;
}
}
finish();
class DelayAsyncTask extends AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... params) {
try {
Thread.sleep(5000);
} catch(Exception e){
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
sleeping=false;
}
}
I am using a variable "sleeping" to let the main thread to wait for the delay.
Just from taking a guess this loop is somewhere in a UI thread. That means that you are blocking the UI thread until the async finishes. That is a NO-NO! Why not just update the image in the onPostExecute? Something like
class DelayAsyncTask extends AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... params) {
try {
Thread.sleep(5000);
} catch(Exception e){
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
ivActionIcon.setImageResource(R.drawable.new_image);
}
obviously remove this line of code as well (I don't think I ever know a reason for doing something like that anyway).
while(sleeping){};
I'm new to Android development. I've be working on Swing and SWT for several years. Both Swing and SWT has a stratage to execute code in UI thread sync and async. The typical usage is doing some time-consume staff in one thread then display the result in UI thread async.
So my question is, is there similiar stratage in Android? Here is my code. Parameter runnable is some time-consume code. This method will display a waiting dialog during the execution then EXPECT to show a Toast after it is finished. But the Toast need to be show in UI thread. So how to do that?
public static void showWaitingDialog(final Activity parent, final Runnable runnable, String msg) {
if (StringUtils.isEmpty(msg)) {
msg = "processing...";
}
final ProgressDialog waitingDialog = ProgressDialog.show(parent, "Please Wait...", msg, true);
// execute in a new thread instead of UI thread
ThreadPoolUtil.execute(new Runnable() {
public void run() {
try {
// some time-consume operation
runnable.run();
} catch (Exception e) {
e.printStackTrace();
} finally {
waitingDialog.dismiss();
}
// TODO: How to display a Toast message here? execute some code in UI Thread.
}
});
}
And is there some words about Android UI system? Such as is it Thread-Safe, how thread works together and so on. Many Thanks!
There are several ways for doing that,
AsyncTask -
AsyncTask enables proper and easy use of the UI thread. This class
allows to perform background operations and publish results on the UI
thread without having to manipulate threads and/or handlers. Example for using AsyncTask
Service -
A Service is an application component representing either an
application's desire to perform a longer-running operation while not
interacting with the user or to supply functionality for other
applications to use. Example for Using Service.
IntentService -
IntentService is a base class for Services that handle asynchronous
requests (expressed as Intents) on demand. Clients send requests
through startService(Intent) calls; the service is started as needed,
handles each Intent in turn using a worker thread, and stops itself
when it runs out of work. Example for using IntentService.
You can use AsyncTask like this.
To call AsyncTask
new getAsynctask().execute("");
and here is the class for geting result.
class getAsynctask extends AsyncTask<String, Long, Integer> {
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(Pass.this, null, "Please wait...");
}
protected Integer doInBackground(String... params) {
try {
// do your coding
return null;
} catch (Exception e) {
return null;
}
}
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
try {
if (loading != null && loading.isShowing())
loading.dismiss();
} catch (Throwable t) {
Log.v("this is praki", "loading.dismiss() problem", t);
}
}
}
Whenever you are working with Separate thread which is not your UI thread the best way is to use Handler. Whenever you want to intimate user from your Thread, suppose a progress then send a message to Handler to so. Inside Handler you can handle message and write a code snippet to Change anything on UI. This is the preferred way for Android. see these link1 , link2 & link3
You use this AsynTask as a inner class of your activity. In do in background do the time consuming task you want to do and then in on postexecute you can show the text message.
call this from your main activity
initTask = new InitTask();
initTask.execute(this);
protected class InitTask extends AsyncTask<Context, Integer, String> {
#Override
protected String doInBackground(Context... params) {
// Do the time comsuming task here
return "COMPLETE!";
}
// -- gets called just before thread begins
#Override
protected void onPreExecute() {
super.onPreExecute();
}
// -- called from the publish progress
// -- notice that the datatype of the second param gets passed to this
// method
#Override
protected void onProgressUpdate(Integer... values) {
}
// -- called if the cancel button is pressed
#Override
protected void onCancelled() {
super.onCancelled();
}
// -- called as soon as doInBackground method completes
// -- notice that the third param gets passed to this method
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Show the toast message here
}
}
Use a handler:
static final int SHOW_TOAST = 0;
public static void showWaitingDialog(final Activity parent, final Runnable runnable, String msg) {
if (StringUtils.isEmpty(msg)) {
msg = "processing...";
}
final ProgressDialog waitingDialog = ProgressDialog.show(parent, "Please Wait...", msg, true);
// execute in a new thread instead of UI thread
ThreadPoolUtil.execute(new Runnable() {
public void run() {
try {
// some time-consume operation
runnable.run();
} catch (Exception e) {
e.printStackTrace();
} finally {
waitingDialog.dismiss();
}
handler.sendMessage(handler.obtainMessage(SHOW_TOAST));
}
});
}
public Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_TOAST:
//Toast here
break;
}
}
};
The Painless threading article from the android developer resources provides different alternatives depending on the specific SDK version.