實(shí)例講解Android多線程應(yīng)用開發(fā)中Handler的使用
其實(shí)可以理解Handler為主線程和另外的線程之間進(jìn)行數(shù)據(jù)更新的東東,并且Handler在主線程中,并在Handler直接調(diào)用線程的run方法
package com.Handler02;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
public class Handler02Activity extends Activity {
/** Called when the activity is first created. */
private Handler handler=new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler.post(thread1);
setContentView(R.layout.main);
System.out.println("================Main==============="+Thread.currentThread().getId());
System.out.println("============Main========="+Thread.currentThread().getName());
}
Runnable thread1=new Runnable() {
@Override
public void run() {
System.out.println("======thread1==============="+Thread.currentThread().getId());
System.out.println("========thread1============"+Thread.currentThread().getName());
try {
Thread.sleep(10000);
} catch (Exception e) {
// TODO: handle exception
}
}
};
}
結(jié)果是主線程等待10s后才顯示出來,并且線程ID
package com.Handler4;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
public class Handler4Activity extends Activity {
/** Called when the activity is first created. */
private TextView textView;
private MyHandler myHandler=new MyHandler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView=(TextView)this.findViewById(R.id.textView1);
}
class MyHandler extends Handler{
//從消息隊(duì)列中取出并handleMessage處理消息
@Override
public void handleMessage(Message msg) {
textView.setText((String)msg.obj);
}
}
public void download(View view){
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
try {
String s="zhangzhao";
Thread.sleep(5000);
//數(shù)據(jù)發(fā)送出來
//textView.setText(s);
Message message=Message.obtain();//最好不要new,這個(gè)obtain會(huì)節(jié)省資源
message.obj=s;
myHandler.sendMessage(message);//沒有指定looper那么就會(huì)發(fā)送給主線程中的looper
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
thread.start();//會(huì)出錯(cuò)原因是在非UI線程里面直接操作UI
//主線程維護(hù)了一個(gè)消息對(duì)列,當(dāng)其他的線程有數(shù)據(jù)需要傳遞給主線程的時(shí)候,你就把數(shù)據(jù)封裝成一個(gè)Message對(duì)象,然后放在主線程的
//消息隊(duì)列中去,對(duì)應(yīng)消息的處理由Looper,子線程通過Handler把下載完的數(shù)據(jù)封裝到Message里面,然后把消息取出來交給Handler進(jìn)行處理
//通過Handle和Message實(shí)現(xiàn)兩個(gè)線程之間達(dá)到共享數(shù)據(jù)
}
}
package com.Handler5;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Handler5Activity extends Activity {
/** Called when the activity is first created. */
private ProgressBar progressBar;
private Button button;
private TextView textView;
private MyHandler myHandler=new MyHandler();
int i=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar = (ProgressBar)this.findViewById(R.id.progressBar1);
textView=(TextView)this.findViewById(R.id.textView1);
}
class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
int pos =(Integer)msg.obj;
progressBar.setProgress(pos);
textView.setText(pos+"%");
}
}
public void downLoad(View view){
new Thread(new Runnable() {
@Override
public void run() {
while(i<=100){
try {
Thread.sleep(300);
i+=10;
Message message=Message.obtain();
message.obj=i;
myHandler.sendMessage(message);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
}
效果:

相關(guān)文章
Android開發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能,涉及Android開發(fā)中的計(jì)時(shí)器相關(guān)組件布局、調(diào)用、事件響應(yīng)等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
Android仿UC底部菜單欄實(shí)現(xiàn)原理與代碼
最近剛看完ViewPager,開始我打算用自定義的imgBtn,但是發(fā)現(xiàn)放在pager選項(xiàng)卡中不好排版,所以最好選了GridView,接下來介紹底部菜單欄實(shí)現(xiàn)2013-01-01
Android?利用ImageView屬性實(shí)現(xiàn)選中和未選中效果
這篇文章主要介紹了Android巧用ImageView屬性實(shí)現(xiàn)選中和未選中效果,實(shí)現(xiàn)思路通常我們會(huì)選擇在布局里加個(gè)ImageView,然后通過代碼層面加個(gè)判斷去讓ImageView加載不同狀態(tài)的圖片,需要的朋友可以參考下2023-06-06
Android自定義view實(shí)現(xiàn)圓形與半圓形菜單
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)圓形與半圓形菜單的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android 使用registerReceiver注冊(cè)BroadcastReceiver案例詳解
這篇文章主要介紹了Android 使用registerReceiver注冊(cè)BroadcastReceiver案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android RelativeLayout相對(duì)布局屬性簡(jiǎn)析
在Android應(yīng)用開發(fā)過程中,為了界面的美觀考慮,經(jīng)常會(huì)使用到布局方面的屬性,本文就以此問題深入解析,詳解一下Android RelativeLayout相對(duì)布局屬性在實(shí)際開發(fā)中的應(yīng)用,需要的朋友可以參考下2012-11-11
詳解Android中使用OkHttp發(fā)送HTTP的post請(qǐng)求的方法
OkHttp(github.com/square/okhttp)是近來人氣迅速攀升的一款第三方安卓HTTP支持包,這里我們就來詳解Android中使用OkHttp發(fā)送HTTP的post請(qǐng)求的方法2016-07-07
Android基礎(chǔ)之startActivityForResult()的用法詳解
這篇文章主要給大家介紹了Android中startActivityForResult()的用法,文中給出了詳細(xì)的介紹與示例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定參考借鑒價(jià)值,有需要的朋友們下面來一起看看吧。2017-01-01

