I programmed an algorithm, which needs a lot of performance. I think this is also the reason why my android app shows a 'app not responding' dialog when there are too many objects to process. With less objects to process the algorithm works perfectly fine. So I started implementing ASyncTask (never done this before), which should call the function to start the algorithm from doInBackground(). (Function to start the algorithm is fillFilteredGraphics(int position))
private class AsyncTaskRunner extends AsyncTask<Void, Integer, Void>{
ProgressDialog progressDialog;
@SuppressLint("WrongThread")
@Override
protected Void doInBackground(Void... voids) {
//This is the important call
mGraphicOverlay.fillFilteredGraphics(0);
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(RecognizerActivity.this,
"ProgressDialog",
"Processing results...");
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
}
And in onCreate():
AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute();
But it still shows the 'App doesn't respond' Dialog
The object mGraphicsOverlay is defined in onCreate():
GraphicOverlay mGraphicOverlay = findViewById(R.id.graphic_overlay);
As you can see the custom class GraphicOverlay extends View
public class GraphicOverlay extends View {
public GraphicOverlay(Context c, AttributeSet attrs){
super(c, attrs);
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//do stuff
}
public void fillFilteredGraphics(int position){
//start algorithm
}
}
There was also a error message, which says
Method fillFilteredgraphics mut be called from the UI thread, currently inferred thread is worker thread
So I added @SuppressLint("WrongThread")
on top of the doInBackground() function
But it doesn't work either. So what do I have to do to get no 'app not responding' dialog, when processing many objects.
@SuppressLint() is just removes the warning. the problem is you are calling UI object view updates from worker thread, but you have to call any UI object from only main thread.
so in your case find any UI object relation where you use to update them in GraphicOverlay class, and if possible divide your background thread too.