Answer the question
In order to leave comments, you need to log in
Why is The application may be doing too much work on its main thread if "much work" is done in a separate thread?
The application has a function for updating the database from the server, in which json is parsed and written to the database. This business is performed for 4-5 seconds, so you need to make a progress indicator.
Settled on a solution with ProgressDialog
.
First did something like this
public class AssetListActivity extends ActionBarActivity {
//код неполный
public boolean onOptionsItemSelected(MenuItem item) {
try {
String res = new ClientAsset(mDBConnector).execute(language).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
this.recreate();
return true;
return super.onOptionsItemSelected(item);
}
public class ClientAsset extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
Indicator.setMessage("прогресс");
Indicator.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Indicator.setProgress(0);
Indicator.setMax(100);
Indicator.show();
//до обновления индикатора пока не дошел
}
@Override
protected String doInBackground(String... params) {
//тут долгая операция
}
}
}
doInBackground
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Indicator.setMessage("прогресс");
Indicator.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Indicator.setProgress(0);
Indicator.setMax(100);
Indicator.show();
try {
String res = new ClientAsset(mDBConnector).execute(language).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
this.recreate();
return true;
}
return super.onOptionsItemSelected(item);
}
onPostExecute
, AndroidStudio says there is no such method at all.Answer the question
In order to leave comments, you need to log in
Why are you calling your AsyncTask's get method? It blocks your UI thread. You must call execute and wait for the result in onPostExecute. There is an example in the documentation , no need to reinvent the wheel.
AndroidStudio says everything correctly: there is no onPostExecute(Void) method, in your case there is onPostExecute(String). Read about generics.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question