1丛忆、Activity的?runOnUiThread ??
textView = (TextView) findViewById( R.id.tv );
???????new?Thread(new?Runnable() {
???????????@Override
???????????public?void?run() {
???????????????runOnUiThread(new?Runnable() {
???????????????????@Override
???????????????????public?void?run() {
???????????????????????textView.setText(?"更新UI了");
???????????????????}
???????????????});
???????????}
???????}).start();
android Activity runOnUiThread() 方法使用
2、Handler sendEmptyMessage()
package?lib.com.myapplication;
import?android.os.Handler;
import?android.os.Message;
import?android.support.v7.app.AppCompatActivity;
import?android.os.Bundle;
import?android.widget.TextView;
public?class?MainActivity?extends?AppCompatActivity {
????private?TextView textView ;
????Handler handler =?new?Handler( ) {
????????@Override
????????public?void?handleMessage(Message msg) {
????????????super.handleMessage(msg);
????????????textView.setText(?"Ui更新了");
????????}
????};
????@Override
????protected?void?onCreate(Bundle savedInstanceState) {
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.activity_main);
????????textView = (TextView) findViewById( R.id.tv );
????????new?Thread(new?Runnable() {
????????????@Override
????????????public?void?run() {
????????????????try?{
????????????????????Thread.sleep(?2000?);
????????????????}?catch?(InterruptedException e) {
????????????????????e.printStackTrace();
????????????????}
????????????????handler.sendEmptyMessage(?2?) ;
????????????}
????????}).start();
????}
}
3吕晌、Handler ?post()
package?lib.com.myapplication;
import?android.os.Bundle;
import?android.os.Handler;
import?android.support.v7.app.AppCompatActivity;
import?android.widget.TextView;
public?class?MainActivity?extends?AppCompatActivity {
????private?TextView textView ;
????Handler handler =?new?Handler();
????@Override
????protected?void?onCreate(Bundle savedInstanceState) {
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.activity_main);
????????textView = (TextView) findViewById( R.id.tv );
????????new?Thread(new?Runnable() {
????????????@Override
????????????public?void?run() {
????????????????try?{
????????????????????Thread.sleep(?2000?);
????????????????}?catch?(InterruptedException e) {
????????????????????e.printStackTrace();
????????????????}
????????????????handler.post(new?Runnable() {
????????????????????@Override
????????????????????public?void?run() {
????????????????????????textView.setText(?"Ui更新了");
????????????????????}
????????????????}) ;
????????????}
????????}).start();
????}
}
在子線程中切換到主線程
new?Thread(new?Runnable() {
????@Override
????public?void?run() {
????????LogUtil.d(?"ttt? 11111111111"?+? Thread.currentThread().getName() );
????????new?Handler(Looper.getMainLooper()).post(new?Runnable() {
????????????@Override
????????????public?void?run() {
????????????????LogUtil.d(?"ttt? 55555555"?+? Thread.currentThread().getName() );
????????????}
????????});
????????LogUtil.d(?"ttt? 22222222222"?+? Thread.currentThread().getName() );
????????LogUtil.d(?"ttt? 33333333333"?+? Thread.currentThread().getName() );
????????LogUtil.d(?"ttt? 44444444444"?+? Thread.currentThread().getName() );
????}
}).start();
結(jié)果
ttt? 11111111111Thread-155
ttt? 22222222222Thread-155
ttt? 33333333333Thread-155
ttt? 44444444444Thread-155
ttt? 55555555main
可見這種方式可以快速切換線程,從log日志來看临燃,切換到主線程不會阻塞子線程睛驳。
4、view Post()
textView = (TextView) findViewById( R.id.tv );
???????new?Thread(new?Runnable() {
???????????@Override
???????????public?void?run() {
???????????????try?{
???????????????????Thread.sleep(?2000?);
???????????????}?catch?(InterruptedException e) {
???????????????????e.printStackTrace();
???????????????}
???????????????textView.post(new?Runnable() {
???????????????????@Override
???????????????????public?void?run() {
???????????????????????textView.setText(?"Ui更新了");
???????????????????}
???????????????}) ;
???????????}
???????}).start();
總結(jié):
1膜廊、其實上面的四種方式都可歸結(jié)于一種方式:handler 用于Android線程之間的通信乏沸。
2、為什么android要求只能在UI線程進行UI操作爪瓜? 主要還是為了避免多線程造成的并發(fā)的問題蹬跃。在單線程操作UI是安全的。