问题
I am a beginner to Android
and I have some confusions regarding Android UI Thread
. Now, I know that no thread apart from the one that created the UI can modify it.
Great.
Here is the Activity
from my first Android app which slightly confuses me.
public class NasaDailyImage extends Activity{
public ProgressDialog modalDialog = null;
//------------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState){
//Instantiate progress dialog, skipping details.
Button b = //get reference to button
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
modalDialog.show(); // show modal
Toast.makeText(getApplicationContext(), "Getting feeds", 500).show();
new AsyncRetriever().execute(new IotdHandler()); // Get the feeds !!
}
});
}
//------------------------------------------------------------------------------
public synchronized void resetDisplay(boolean parseErrorOccured,
boolean imageErrorOccured,
IotdHandler newFeeds){
if(parseErrorOccured || imageErrorOccured){
// make a Toast
// do not update display
}else{
// make a Toast
// update display
// based on new feed
}
}
//------------------------------------------------------------------------------
class AsyncRetriever extends AsyncTask<IotdHandler,Void,IotdHandler>{
@Override
protected IotdHandler doInBackground(IotdHandler... arg0) {
IotdHandler handler = arg0[0];
handler.processFeed(); // get the RSS feed data !
return handler;
}
//------------------------------------------------------------------------------
@Override
protected void onPostExecute(IotdHandler fromInBackground){
resetDisplay( // call to update the display
fromInBackground.errorOccured,
fromInBackground.imageError,
fromInBackground);
}
//------------------------------------------------------------------------------
}
1.
onCreate
is on the UI thread so I can do whatever I want but onClick
is not. Why can I make a ProgressDialog
and a Toast
in that method? Why no error there?2. The
AsyncTask
is subclass of the the NasaDailyImage
. This means it can access all the methods of NasaDailyImage
including resetDisplay()
which updates the display. resetDisplay()
is called in the onPostExecute
which runs on a different thread from UI. So, why can I update the display there and yet get no errors ?
回答1:
onClick()
is indeed on the UI thread. Most of what happens in an Activity happens on the UI thread.onPostExecte()
(and its counterpartonPreExecute()
) runs on the UI thread as well. The AsyncTask.onPostExecte() documentation clearly states this. AsyncTask was deliberately designed such that developers could update the UI before and after they do background work.
In general, your code will be running on the UI thread unless you explicitly tell it otherwise. Once you create AsyncTasks, Runnables, or Threads, you need to ensure you understand where your code is executing. In an Activity, it is typically safe to assume you are on the UI thread.
回答2:
You are extending AsyncTask class , where async task class is calling its sequential method automatically. First
onPreExecute
thendoBackground
and finallyonPost
. If you want to change any ui change you can useonProgressUpdate
method.To use your activity class simple call
activityclass.this.resetDisplay()
. Because inner class scope sometimes failed to integrate except global varible.
Thanks
来源:https://stackoverflow.com/questions/17842747/understanding-the-ui-thread