Reputation: 567
Am doing a heavy server task within Asynctask. As soon as some portion of data is get downloaded i need to call an method who deals with some ui elements on main thread. But its not working for me.The screen remains free zed until it complete the Async task. So how can i make that method run on main thread and my background task should also be running?
Here is my code
public class AsyncgetTestDetailsfromServer extends AsyncTask<Void ,Void,Integer>
{
@Override
protected Integer doInBackground(Void... params) {
Ion.with(TestActivity.this)
.load(AppConstants.GET_TEST_QUESTION_ANSWER+"testID="+Test_id)
.setTimeout(30000)
.asString().setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String response) {
if (e != null) {
e.printStackTrace();
displayAlert(TestActivity.this,
getString(R.string.app_name),
getString(R.string.strInternetSlow), "0");
}
if(response!=null){
try {
JSONArray jsonArray = new JSONArray(response);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
String result = jsonObject.getString("TransactionStatus");
if(result.equals("Success"))
{
JSONArray testArray = (JSONArray) jsonObject.get("TestDetails");
for(int j=0;j<testArray.length();j++)
{
JSONObject obj = (JSONObject) testArray.get(j);
JSONArray SubjectArray = (JSONArray) obj.get("SubjectList");
QueCount=0;
for(int k=0;k<SubjectArray.length();k++)
{
JSONObject SubObj = (JSONObject) SubjectArray.get(k);
JSONArray QuestionArray = (JSONArray) SubObj.get("QuestionList");
if (QuestionArray != null && QuestionArray.length() > 0) {
for(int l=0;l<QuestionArray.length();l++)
{
QueCount++;
// Contains some database operations...
if(QueCount==51)
{
TestActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
DisplayQuestion();
BaseActivity.StopLoadingDialog();
StartTimer();
}
});
}
}
if(QueCount<=50){
DisplayQuestion();
BaseActivity.StopLoadingDialog();
StartTimer();
}
}
else
{
displayAlert(act,
getString(R.string.app_name),
getString(R.string.strNoQuestionFound), "1");
}
}
}
}
if(result.equals("Error"))
{
int ErrorCode = jsonObject.getInt("ERROR");
showErrorMessage(ErrorCode,TestActivity.this);
}
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
});
return QueCount;
}
}
and called at
new AsyncgetTestDetailsfromServer().execute();
Upvotes: 0
Views: 418
Reputation: 390
I don't really know how your callback is working but the screen might freeze because of it. Asynctask implements a onPostExecute method that in which you can use a callback.
Callback interface :
public interface AsyncTaskCompleteListener<T> {
public void onTaskComplete(T result, int number);
}
AsyncTask :
public class LoadURL extends AsyncTask<String, Process, String> {
private AsyncTaskCompleteListener<String> callback;
public LoadURL(AsyncTaskCompleteListener<String> cb) {
this.callback = cb;
}
protected void onPreExecute() {}
protected String doInBackground(String... arg0) {
// do something
return content;
}
protected void onPostExecute(String content) {
if (callback != null)
callback.onTaskComplete(content,number);
}
}
Activity :
public class LoginActivity extends Activity implements AsyncTaskCompleteListener<String> {
@Override
protected void onCreate(Bundle savedInstanceState) {
LoadURL loadUrl = new LoadURL(LoginActivity.this);
loadUrl.execute(...);
}
@Override
public void onTaskComplete(String result, int number) {...}
}
I never had any freeze problem with thise code. Hope this will help
Upvotes: 1
Reputation: 3735
When you have callback
function then why you doing work in background just remove asyncTask
code, start from below code,
Your code will work without
asyncTask
as i can see because ofCallBack
method.
Ion.with(TestActivity.this)
.load(AppConstants.GET_TEST_QUESTION_ANSWER+"testID="+Test_id)
.setTimeout(30000)
.asString().setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String response) {
if (e != null) {
e.printStackTrace();
displayAlert(TestActivity.this,
getString(R.string.app_name),
getString(R.string.strInternetSlow), "0");
}
..................
,,,,,
...................
....................
Use async inside
if(response!=null){
try {
//Use asyncTask here...
}
Upvotes: 1