I have created an asynchronous task like this:
private class LongOperationcheckall extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String result="Start";
try
{
Looper.myLooper().prepare();
// Looper.loop();
// TODO Auto-generated method stub
for(int k=0;k<13;k++)
{
checkall();
this.publishProgress("Show the dialog");
//count++;
}
result="Success";
checkallcomp++;
OnscanComplete();
}
catch(Exception ex)
{
checkallcomp++;
OnscanComplete();
}
return result;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Log.i("Progress", "Progressincheck");
}
protected void onPostExecute(String params ) {
Log.w("all Check",params);
// Execution of result of Long time consuming operation
}
}
I have added code,
Looper.myLooper().prepare();
to call to this task as
taskAllcheck = new LongOperationcheckall();
taskAllcheck.execute();
The fourth time when I click on start button the fourth time, it gives the exception
Can’t create handler inside the thread inside thread which not called looper.prepare
Only, when I run in the device it executes OK in the emulator, but after add this line, the error comes as only one looper may be created per thread when I click on the start button for the fourth time and call taskAllcheck.execute();.
Please think of the asynchronous thread as a UI thread that has a inbuilt handler to update your UI. No matter what, it is a thread. So now that is cleared, I don’t see where you have actually written the
checkall()function. The check all function needs to be a local methodology for the asynchronous task to call in the background. It cannot be a method in your main class. If that is the case, there is no point of having a asynchronous task. So please add thecheckall()function to your asynchronous task.Next, there is no need of the looper in the asynchronous task. The handling of the data is taken care of by:
Please make sure that you pass in anything that you want. Either an
int,string,boolean,arrayor anything and everything can be handled in this. So make wise use of the implementation.Now, make the inside the
onProgressUpdate()correspondingly and hence also please check that if you want something from the UI thread to be executed, such as a methods or something. That can be done in this method.Now make the changes I have told you. Please get back to me and I will be in a better position to work with you. If we can pinpoint the problem. Take care.