Android: AsyncTask: Long execution time after calling onPostExecute - java

I have a simple Android AsyncTask with a long background action:
HANDLER.post(new Runnable() {
#Override
public void run() {
new AsyncTask<Context, Integer, GameTypeAdapter>() {
#Override
protected GameTypeAdapter doInBackground(Context... contexts) {
return new GameTypeAdapter(contexts[0]);//Long database action
}
#Override
protected void onPreExecute() {
pbWaiter.setVisible(View.VISIBLE);
}
#Override
protected void onPostExecute(GameTypeAdapter gameTypeAdapter) {
txtGameName.setText(settings.getGameName());
cmbType.setAdapter(gameTypeAdapter);
if (settings.getGamePlugin() != null) {
cmbType.setSelection(((GameTypeAdapter) cmbType.getAdapter()).getItemPosition(settings.getGamePlugin()));
}
sldJoker.setProgress(settings.getJoker());
pbWaiter.setVisible(View.INVISIBLE);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getActivity());
}
});
If the method onPostExecute is called by thread and the last line (hideWaiter) was called the execution of the UI calls are visible after 1/10 seconds (a half second in debug mode). In this time the UI hangs and the waiter animation stops.
Why the UI thread stops?
Thanks.

Related

Does onPreExecute() and onPostExecute() execute on the UI thread or on the thread from which the AsyncTask has been started?

I have been writing AsyncTask's for short background operations in android for quite some time and had a very basic question. If I start an AsyncTask from a separate thread and not the main UI thread, would my onPreExecute()andonPostExecute methods be still called in the UI thread or the thread from which I started the AsyncTask. I am curious because I was unable to show a popup inside the onPreExecute() method when I started the it from some other thread.
EDIT 2
I tried writing this simple activity to try:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
#Override
public void run() {
final TestAsyncTask task = new TestAsyncTask();
task.execute();
}
}).start();
}
private class TestAsyncTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... voids) {
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(MainActivity.this, "Yo!", Toast.LENGTH_LONG).show();
}
}
}
This runs fine.
But when I run the application with the following code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
#Override
public void run() {
final TestAsyncTask task = new TestAsyncTask();
task.execute();
}
}).start();
}
private class TestAsyncTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this, "Yo!", Toast.LENGTH_LONG).show();
}
#Override
protected Void doInBackground(Void... voids) {
return null;
}
}
}
It fails with the following error:
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
as one of the lines in the stack trace.
While docs say that these callbacks execute in main thread - it is not true. onPreExecute() runs synchronously from the executeOnExecutor() i.e. in the thread that starts AsyncTask.
onPostExecute() is always runs in main thread. (it is invoked from finish(), and this happens inside Handler that uses looper of main thread).
You should do UI updates and alert or pop up showing in AsyncTask's onPostExecute() method which runs on UI thread. The AsyncTask's doinBackground() method runs on another thread.
Quoting offical Docs:
onPostExecute
Added in API level 3
void onPostExecute (Result result)
Runs on the UI thread after doInBackground(Params...). The specified result is the value returned by doInBackground(Params...).
onPreExecute
Added in API level 3
void onPreExecute ()
Runs on the UI thread before doInBackground(Params...).
you can find it here https://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute(Result)
Do in background runs on background thread while OnPreExecute and OnPostExecute run on main Ui thread.

Android : How to Run method in service on different thread?

I trying to do some long time operation in service which is started from the controller.
public void startDashboardBgService() {
Boolean isAppInForeground = Container.getInstance().isAppInForeground();
if (isAppInForeground) {
stopDashboardBgService();
context.startService(new Intent(context, DashboardService.class));
}
}
Then i calling long time operation method from the onStartCommand event.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Logger.d("onStartCommand");
updateDashboardValues();
handler = new Handler();
context = this;
return mStartMode;
}
And here is the method which should to do work on the separated thread.
private Runnable runnableCode = new Runnable() {
#Override
public void run() {
List<Reminder> reminders = Reminder.getAllRemindersFromDB(context);
if (reminders.size() > 0) {
for (Reminder r : reminders) {
Logger.d(r.getMessage());
}
} else {
Logger.d("No reminders in table, dashboard cannot be updated");
}
}
};
private void updateDashboardValues() {
try {
Thread t = new Thread(runnableCode);
t.start();
} catch (Exception e) {
TrackingEventLogHelper.logException(e, Constants.Global.EXCEPTION,
Constants.ExceptionMessage.EXC_CANNOT_CANCEL_UNIQUE_ALARM_FOR_DASHBOARD_CHECK, true);
}
}
But when is the method called from activity (controller) UI is lagging and i getting following message.
Skipped 604 frames! The application may be doing too much work on its main thread.
How can i solve it please?
Many thanks for any advice.
Try running the code inside an AsyncTask, see an example below. Note: you can parameterize it with any types you need.
Edit: example of how to pass parameters (Context in this case) to the task.
new AsyncTask<Context, Void, Void>() {
//local fields
#Override
protected void onPreExecute() {
//runs on UI Thread
}
#Override
protected void onCancelled() {
/* runs on UI Thread instead of onPostExecute()
if cancel(boolean) was called */
}
#Override
protected void onProgressUpdate(Progress... values) {
//runs on UI Thread
}
#Override
protected Void doInBackground(Context... params) {
//background Thread - do heavy work here
Context c = params[0];
return null;
}
#Override
protected void onPostExecute(Void result) {
//runs on UI Thread
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mContext);
You can create a new Thread & a Runnable and wrap your method in it.
Or you can use an IntentService which takes care of running your methods in a worker thread automatically
Android Services by default, run on UI thread. If you have to do some heavy work you need to perform it on worker thread.
It would be better to use IntentService for long running operations, because using AsyncTask for long running operations is not recommended.

Android: “BadTokenException: Unable to add window; is your activity running?

I am opening a progressdialog with AsyncTask in doInBackground method the question is loading from database and after question successfully loaded the progress dialog box will be closed
but my problem is some time I am getting following error
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRoot$W#44757528 is not valid; is your activity running?
by doing some googling I have found that there may be i am holding on to a reference to a Context (either explicitly, or by creating a Dialog or Toast or some other dependent item) that has been destroyed (typically because you are using the onCreateDialog or you passed the Activity to some other process that didn't get destroyed when the Activity was destroyed).
So I have put below code that dismiss progressdialog in-case if activity is destroyed before dialog box is dismissed
protected void onDestroy() {
if (pdForNewQuestion != null)
pdForNewQuestion.dismiss();
super.onDestroy();
}
but I still face the issue. I am not destroying any activity but still the error suddenly comes sometimes and sometimes it works properly
the async code is below
// Start new question in every 60 seconds :)
new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
mProgressStatus++;
} catch (Exception e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
txtCountingNum.setText((timer--) + "\nSec.");
if (timer < 0) {
questionLoadWithAsyncTask();
}
}
});
}
}
}).start();
public void questionLoadWithAsyncTask() {
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
pdForNewQuestion = new ProgressDialog(QuizActivity.this);
pdForNewQuestion.setTitle("Please wait...");
pdForNewQuestion.setMessage("Question is loading...");
pdForNewQuestion.setCancelable(false);
pdForNewQuestion.setIndeterminate(true);
pdForNewQuestion.show();
}
#Override
protected Void doInBackground(Void... arg0) {
wordsCursor = dbHelper.getRandomWords();
return null;
}
#Override
protected void onPostExecute(Void result) {
if (pdForNewQuestion != null) {
pdForNewQuestion.dismiss();
}
}
}.execute();
}
Check whether the dialog is showing or not if dialog is showing then only dismiss like this..
#Override
protected void onDestroy() {
if (pdForNewQuestion != null) {
if (pdForNewQuestion.isShowing()) {
pdForNewQuestion.dismiss();
pdForNewQuestion = null;
}
}
super.onDestroy();
}
you are running infinite loop inside new Thread but not breaking the loop and stopping that Thread. It runs infinitely in the background even when activity goes background. try stopping the Thread once work is finished.
First of all why are you starting your AsyncTask inside a Thread? As i understand you are trying to start an AsyncTask every 60 seconds and populate a new question. There is a much better way to do this using only a Handler and AsyncTask.
Create a Handler and post Runnable which runs every seconds and depending on the result start your AsyncTask :
int mSeconds = 0;
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
if(mSeconds == 60){
new QuestionLoader().execute();
mSeconds = 0;
}
mHandler.postDelayed(this, 1000);
mSeconds++;
}
}, 1000);
and you can create your AsyncTask like this :
private class QuestionLoader extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute(){
super.onPreExecute();
pdForNewQuestion = new ProgressDialog(MainActivity.this);
pdForNewQuestion.setTitle("Please wait...");
pdForNewQuestion.setMessage("Question is loading...");
pdForNewQuestion.setCancelable(false);
pdForNewQuestion.setIndeterminate(true);
pdForNewQuestion.show();
}
#Override
protected Void doInBackground(Void... params) {
wordsCursor = dbHelper.getRandomWords();
return null;
}
#Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
if(pdForNewQuestion != null && pdForNewQuestion.isShowing()){
pdForNewQuestion.dismiss();
}
}
}
This is usually caused by your app trying to display a dialog using a previously-finished Activity as a context. Then check that the activity is not closed by some other apps or other triggers before showing the dialog
if (!isFinishing()) {
//showdialog here
}

Start an Activity from Splash Screen, should I use run() or runOnUiThread()?

I have a Splash Screen (Logo Activity) to show the company name for 3 seconds before app starts. I start Main Activity from a thread, here is the code:
public class Logo extends Activity {
Thread t;
public boolean dead = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logo);
t = new Thread() {
public void run() {
try {
Intent i = new Intent(Logo.this, Main.class);
Thread.sleep(3000);
if (!dead) {
startActivity(i);
}
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();
}
The Main Activity is called from a worked thread, is this correct? What are the differents with this code (using runOnUiThread)?
...
if (!dead) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Logo.this, Main.class);
startActivity(i);
}
});
}
...
I see no difference with this code in debug mode (The same threads, the same operation, etc.). Which is correct?
Starting an intent I think is not an UI operation. runOnUI thread runs UI operation on UI thread. So you can use either of thread (runOnUI or normal). May be normal thread will be good in this situation. But I would like to suggest you use timer instead.
To be honest, I don't like the Thread.sleep. PLease take a look at my solution:
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// Do your work here like... startActivity...
}
}, SPLASH_DURATION); // SPLASH_DURATION IS IN MILLISECONDS LIKE 3000
Also you can block the user to prevent the back key like this:
#Override
public void onBackPressed() {
// do nothing! disable user interaction!
}
You should use AsyncTask in "doInBackground" background thread and than sleep your thread(this thread not UIThread) "PostExecute" run on UI Thread than start your new activity
private class mSplashViewer extends AsyncTask<Void,Void,Void>{
protected void doInBackground(Void params){
Thread.currentThread().sleep(3000);
return null;
}
protected void onPostExecute(){
startActivity(...);
}
}

Show an AlertDialog from a background Thread with the appcontext

Is possible to raise an AlertDialog from a background thread using a reference to getApplicationContext()?
I'm trying with that code but it doesn't work
new Thread(){
public void run(){
new AlertDialog.Builder(appcontext)
.setMessage("Test")
.setPositiveButton("Ok", null)
.show();
}
}.start();
Thanks in advance
No, you do not want to do this. Android does not permit UI work on any thread but the UI-thread because UI code is not thread safe. See "Painless Threading"1.
You can call Activity.runOnUiThread(Runnable) (on the specific activity) from another thread to force the code to run on the UI thread. You can also call View.post(Runnable) (on the specific view) to cause the action to be queued onto the UI thread. For more details on those options and others, see the above mentioned article.
However, Android also provides something called AsyncTask which is specifically designed for running some stuff on a separate thread, and some on the UI thread. This automatically uses Android's threadpool, and if you do not have any reason to use an explicit separate thread, is an easy, clean way to go:
From the Android docs:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
// Runs on a ThreadPool thread
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
// Sends data to onProgressUpdate to run on the UI thread
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
// Runs on the UI thread!
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
// Runs on the UI thread!
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
you cannot access UI elements from a thread, you must create a handler and call it from your thread.
1- handler: to handle UI from other thread
private Handler handler= new Handler() {
public void handleMessage(Message msg){
/*put your code here to update on UI*/
}
};
2- in you thread call this:
Thread t = new Thread(new Runnable() {
#Override
public void run() {
handler.sendEmptyMessage(0);
}
}); //thread
t.start();
Simplest Solution is to use an AsyncTask.
try the following code
private class LaunchDialog extends AsyncTask<Void,Void,Void>{
Context context;
public LaunchDialog(Context ctx){
context = ctx;
}
#Override
protected ArrayList<CategoryObj> doInBackground(Void... params) {
//do the task to be done on NON-UI thread , or NON-Blocking thread
// publishProgress(null);
}
#Override
protected void onProgressUpdate(Void... v){
//stuff done on UI thread , can be invoked from doInBackground
}
#Override
protected void onPostExecute(Void x){
//stuff to be done after task executes(done on UI thread)
new AlertDialog.Builder(context)
.setMessage("Test")
.setPositiveButton("Ok", null)
.show();
}
#Override
protected void onPreExecute(){
//stuff to be done before task executes (done on UI thread)
}
}
to start the thread just do
new LaunchDialog(this).execute();
read article about painless threading here -- http://developer.android.com/resources/articles/painless-threading.html
You'll have to get access to the UI thread, any of these: View.post(), Activity.runOnUiThread(), or obtaining and sending a Message.
You cannot change UI in background thread. There is another thread to perform UI activities
runOnUiThread(new Runnable() {
#Override
public void run() {
//perform UI operations
}
});

Categories