Original article, if reproduced, please indicate the source: http://blog.csdn.net/yihui823/article/details/6722784
This is a simple statement I have extracted:
The UI display cannot be modified in a non-UI master thread.
The main meaning is that in the UI control thread, our code can randomly change the Display Effect of each UI object, including the text, whether it is visible, size, and other attributes.
What is a master thread?
To put it simply, the main control thread is used to access functions called by systems such as oncreate and onresume.
Of course, it is called by the system, not by our own code to call functions such as oncreate.
This also includes event listening for buttons and other controls, such as The onclick function.
Therefore, the Code not called in these functions is not the UI Main Control thread.
At this time, the system will throw an exception:
Android. View. viewroot $ calledfromwrongthreadexception: only the original thread that created a view hierarchy can touch its views.
This restriction also exists in Java's swing. If any thread is allowed to change the UI display, the screen will be messy.
The following is an error code:
public class TemppjActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); final Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { btn.setText("I Clicked!"); } }); final DateFormat f = DateFormat.getDateTimeInstance(); TimerTask t = new TimerTask() { @Override public void run() { btn.setText(f.format(new Date())); } }; Timer ti = new Timer(); ti.schedule(t, 0, 5000); }}
Looking at the red line is to display the text of the buttons in a scheduled thread. This program runs with a calledfromwrongthreadexception.
So what if we have this requirement? Can this be done? Of course, this can be done. At this time, we need to use: HAndler
Let's first look at what code needs to be changed:
public class TemppjActivity extends Activity { private Handler handle; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); final Button btn = (Button) findViewById(R.id.btn); final DateFormat f = DateFormat.getDateTimeInstance(); handle = new Handler() { public void handleMessage(Message msg) { btn.setText(f.format(new Date())); }; }; btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { btn.setText("I Clicked!"); } }); TimerTask t = new TimerTask() { @Override public void run() { handle.sendEmptyMessage(0); } }; Timer ti = new Timer(); ti.schedule(t, 0, 5000); }}
That is to say, in a non-UI master thread, if you need to modify the UI, a message is sent to the UI master interface.
Handle. sendemptymessage (0 );
Messages can be very complex. Here we only send an empty message.
In the main control thread, Handler processes these messages, receives the messages, and then processes the changes of various controls.
In the handlemessage function, we can specifically control how the UI should be changed.