I have a database update operation that has activity that continues to update the percentage and runs inside AsyncTask.
Inside doInBackground (), I call the controller, which updates the database and constantly updates the percentage of activity, however, if I click the "Home" or "Back" button, the operation is canceled. What are you suggesting me to do?
I tried to start the service inside doInBackground () so that it works in the background, but it looks like it is not working.
My code is as follows:
public class UpdateDatabaseAsyncTask extends AsyncTask<Void, Integer, Integer> { @Override public void onPreExecute() { mCustomProgressBar.startAnimation(); } @Override public Integer doInBackground(Void... params) { return mController.updateDatabase(); } @Override public void onPostExecute(Integer result) { mCustomProgressBar.stopAnimation();
And inside the controller, I call the callPublishProgress(value)
method, which passes the current percentage value, so it will publishProgress(value)
in the user interface.
I debugged and I pressed the home / back button and it just stopped the workflow.
Another solution that I tried started the Service to start in the background, regardless of how the user presses the home / back button or not, so I thought, and the Service would make a call to the controller method that does this work, and this will call callPublishProgress(value)
to update the percentage value in the user interface.
However, what happens, the code reaches doInBackground()
and starts the service, but it immediately goes to onPostExecute()
, it just did not wait for the service to finish (of course! ). Therefore, it gives a NullPointerException
. I was thinking of creating a loop inside doInBackground()
with the flag set in the Service, so it would leave this loop until the service was finished (I used IntentService), but this did not work.
I also thought about using a timer. But I do not know.
I read articles in Threads documentation, etc. And he suggests using AsyncTask
, as I tried. It also talks about runOnUiThread(Runnable)
.
In any case, I need to do the operation in the background (perhaps using IntentService), so regardless of whether the user clicks the home button, he will continue to work, but he must update the percentage of the user interface, and when the user leaves the screen and returns to it, it shows the current percentage value updated on the screen.
What is the best solution for my case?
Thanks.