Anndroid學習---使用執行緒Handler
阿新 • • 發佈:2018-11-01
Anndroid學習—使用執行緒 Handler
使用Java裡面常用的執行緒呼叫方式,發現不可以
1 .
package com.example.administrator.myapplication;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText2);
button = (Button) findViewById(R.id.button);
/* handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
editText.setText("star Thread");
}
};*/
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// editText.setText("sss");
MyThread myThread = new MyThread();
myThread.start();
}
});
}
class MyThread extends Thread{
@Override
public void run() {
// super.run();
editText.setText("star Thread");
/* try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
// handler.sendEmptyMessage(0);
}
}
}
2 . 分析錯誤
E/AndroidRuntime: FATAL EXCEPTION: Thread-192
Process: com.example.administrator.myapplication, PID: 3528
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6357)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:909)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4690)
Android UI操作並不是執行緒安全的並且這些操作必須在UI執行緒中執行,如果在非UI執行緒中直接操作UI執行緒,
會丟擲android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that
created a view hierarchy can touch its views,這與普通的java程式不同。
由於UI執行緒負責事件的監聽和繪圖,因此,必須保證UI執行緒能夠隨時響應使用者的需求,
UI執行緒裡的操作應該向中斷事件那樣短小,費時的操作(如網路連線)需要另開執行緒,
否則,如果UI執行緒超過5s沒有響應使用者請求,會彈出對話方塊提醒使用者終止應用程式(ANP)。
如果在新開的執行緒中需要對UI進行設定,就可能違反單執行緒模型,
因此android採用一種的Message Queue機制保證執行緒間通訊。
可以在自己的子執行緒中建立Handler與UI thread通訊
執行緒的簡單使用-點選按鈕文字框顯示指定內容
package com.example.administrator.myapplication;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
Handler handler;
MyThread myThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText2);
button = (Button) findViewById(R.id.button);
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
editText.setText("star Thread");
}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// editText.setText("sss");
myThread = new MyThread();
myThread.start();
}
});
}
class MyThread extends Thread{
@Override
public void run() {
while(true){
try {
Thread.sleep(1000);
handler.sendEmptyMessage(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
用thread handler 實現圖片變換
package com.example.administrator.myapplication;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
Button button2;
Handler handler;
ImageView imageView;
MyThread myThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText2);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.imageView);
button2= (Button) findViewById(R.id.button2);
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
switch (msg.what){
case 1 :
imageView.setImageResource(R.mipmap.ic_launcher);
break;
case 2 :
imageView.setImageResource(R.mipmap.abc_ic_star_black_16dp);
break;
}
// editText.setText("star Thread");
}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// editText.setText("sss");
myThread = new MyThread();
myThread.is= true;
myThread.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myThread.is= false;
}
});
}
class MyThread extends Thread{
boolean is=true;
@Override
public void run() {
int what = 1;
while(is){
try {
Thread.sleep(1000);
handler.sendEmptyMessage((what++)%2+1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}