//第一种使用
new Thread() {
public void Run() {
try {
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO 自动生成的方法存根
TxtView.setText("asdasd");
}
});
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}.start();;
//第二种
MyRunable mMyRunable=new MyRunable();
public class MyRunable implements Runnable{
@Override
public void run() {
ImageIndex++;
ImageIndex=ImageIndex%3;
Img.setImageResource(ImageID[ImageIndex]);
mHandler.postDelayed(mMyRunable, 1);
}
}
mHandler.postDelayed(mMyRunable, 1);
Handler mHandler3=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
Toast.makeText(getApplicationContext(), ""+1, Toast.LENGTH_SHORT).show();
return false;
}
}){
public void handleMessage(Message msg){
Toast.makeText(getApplicationContext(), ""+2, Toast.LENGTH_SHORT).show();
}
};
mHandler3.sendEmptyMessage(10000);
Handler mHandler2=new Handler(){
@Override
public void handleMessage(Message msg) {
TxtView.setText(msg.arg1+"-"+msg.arg2+"-"+msg.obj);
}
};
Message message=new Message();
message.arg1=88;
message.arg2=100;
message.obj=100;//任意OBJECT类型 实体类
mHandler2.sendMessage(message);
Hnader Looper MessageQueue 之间的关系
Hnader负责发送消息 Looper负责接收Hnader发送的消息 并直接把消息回传给Hnader自己,MessageQueue就是一个储存消息的容器
HandlerThread 的用法
private TextView mTextView;
private HandlerThread mHandlerThread;
private Handler mHandler;
mTextView=(TextView)findViewById(R.id.id_txt);
mHandlerThread=new HandlerThread("ThreadName");
mHandlerThread.start();
mHandler=new Handler(mHandlerThread.getLooper()){
@Override
public void handleMessage(Message msg) {
mTextView.setText("ThreadName------------"+Thread.currentThread());
}
};
mHandler.sendEmptyMessage(1);
private TextView mTextView;
private TextView mTextView2;
private Button Btn1;
private Button Btn2;
// 主线程的Handler
private Handler MainHandler = new Handler() {
public void handleMessage(Message msg) {
Message msgMessage =new Message();
mTextView2.setText("住线程");
threadHandler.sendMessageDelayed(msgMessage, 1000);
};
};
// 子线程Handler
private Handler threadHandler;
mTextView = (TextView) findViewById(R.id.id_txt);
mTextView2 = (TextView) findViewById(R.id.id_txt2);
Btn1 = (Button) findViewById(R.id.button1);
Btn2 = (Button) findViewById(R.id.button2);
HandlerThread mHandlerThread = new HandlerThread("MainThread");
mHandlerThread.start();
threadHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Message msgMessage =new Message();
mTextView2.setText("子线程");
MainHandler.sendMessageDelayed(msgMessage, 1000);
}
};
Btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MainHandler.sendEmptyMessage(1);
}
});
Btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MainHandler.removeMessages(1);
threadHandler.removeMessages(1);
}
});
//第一种
private Handler mHandler=new Handler(){
public void handleMessage(Message msg) {
};
};
//第一种 通过Hnadler更新UI
new Thread(){
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
mHandler.post(new Runnable() {
@Override
public void run() {
mTextView.setText("asdasd");
}
});
};
}.start();
private Handler mHandler=new Handler(){
public void handleMessage(Message msg) {
mTextView.setText("asdasd");
};
};
new Thread(){
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Handler2();
};
}.start();
public void Handler2(){
mHandler.sendEmptyMessage(1);
}
//第三种是用内置函数 在子线程中直接调用
public void UpdayeUI()
{
runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText("asdasd");
}
});
}
//第四种 使用控件自带的方法更新 在子线程中直接调用
public void ViewUI(){
mTextView.post(new Runnable() {
@Override
public void run() {
mTextView.setText("asdasd");
}
});
}