日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 实例源码解释,Android Handler 原理分析及实例代码

發(fā)布時間:2025/3/8 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 实例源码解释,Android Handler 原理分析及实例代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Android Handler 原理分析

Handler一個讓無數(shù)android開發(fā)者頭疼的東西,希望我今天這邊文章能為您徹底根治這個問題

今天就為大家詳細剖析下Handler的原理

Handler使用的原因

1.多線程更新Ui會導致UI界面錯亂

2.如果加鎖會導致性能下降

3.只在主線程去更新UI,輪詢處理

Handler使用簡介

其實關鍵方法就2個一個sendMessage,用來接收消息

另一個是handleMessage,用來處理接收到的消息

下面是我參考瘋狂android講義,寫的一個子線程和主線程之間相互通信的demo

對原demo做了一定修改

public class MainActivity extends AppCompatActivity {

public final static String UPPER_NUM="upper_num";

private EditText editText;

public jisuanThread jisuan;

public Handler mainhandler;

private TextView textView;

class jisuanThread extends Thread{

public Handler mhandler;

@Override

public void run() {

Looper.prepare();

final ArrayList al=new ArrayList<>();

mhandler=new Handler(){

@Override

public void handleMessage(Message msg) {

if(msg.what==0x123){

Bundle bundle=msg.getData();

int up=bundle.getInt(UPPER_NUM);

outer:

for(int i=3;i<=up;i++){

for(int j=2;j<=Math.sqrt(i);j++){

if(i%j==0){

continue outer;

}

}

al.add(i);

}

Message message=new Message();

message.what=0x124;

Bundle bundle1=new Bundle();

bundle1.putIntegerArrayList("Result",al);

message.setData(bundle1);

mainhandler.sendMessage(message);

}

}

};

Looper.loop();

}

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

editText= (EditText) findViewById(R.id.et_num);

textView= (TextView) findViewById(R.id.tv_show);

jisuan=new jisuanThread();

jisuan.start();

mainhandler=new Handler(){

@Override

public void handleMessage(Message msg) {

if(msg.what==0x124){

Bundle bundle=new Bundle();

bundle=msg.getData();

ArrayList al=bundle.getIntegerArrayList("Result");

textView.setText(al.toString());

}

}

};

findViewById(R.id.bt_jisuan).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Message message=new Message();

message.what=0x123;

Bundle bundle=new Bundle();

bundle.putInt(UPPER_NUM, Integer.parseInt(editText.getText().toString()));

message.setData(bundle);

jisuan.mhandler.sendMessage(message);

}

});

}

}

Hanler和Looper,MessageQueue原理分析

1.Handler發(fā)送消息處理消息(一般都是將消息發(fā)送給自己),因為hanler在不同線程是可使用的

2.Looper管理MessageQueue

Looper.loop死循環(huán),不斷從MessageQueue取消息,如果有消息就處理消息,沒有消息就阻塞

public static void loop() {

final Looper me = myLooper();

if (me == null) {

throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");

}

final MessageQueue queue = me.mQueue;

// Make sure the identity of this thread is that of the local process,

// and keep track of what that identity token actually is.

Binder.clearCallingIdentity();

final long ident = Binder.clearCallingIdentity();

for (;;) {

Message msg = queue.next(); // might block

if (msg == null) {

// No message indicates that the message queue is quitting.

return;

}

// This must be in a local variable, in case a UI event sets the logger

Printer logging = me.mLogging;

if (logging != null) {

logging.println(">>>>> Dispatching to " + msg.target + " " +

msg.callback + ": " + msg.what);

}

msg.target.dispatchMessage(msg);

if (logging != null) {

logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);

}

// Make sure that during the course of dispatching the

// identity of the thread wasn't corrupted.

final long newIdent = Binder.clearCallingIdentity();

if (ident != newIdent) {

Log.wtf(TAG, "Thread identity changed from 0x"

+ Long.toHexString(ident) + " to 0x"

+ Long.toHexString(newIdent) + " while dispatching to "

+ msg.target.getClass().getName() + " "

+ msg.callback + " what=" + msg.what);

}

msg.recycleUnchecked();

}

}

這個是Looper.loop的源碼,實質(zhì)就是一個死循環(huán),不斷讀取自己的MessQueue的消息

3.MessQueue一個消息隊列,Handler發(fā)送的消息會添加到與自己內(nèi)聯(lián)的Looper的MessQueue中,受Looper管理

private Looper(boolean quitAllowed) {

mQueue = new MessageQueue(quitAllowed);

mThread = Thread.currentThread();

}

這個是Looper構(gòu)造器,其中做了2個工作,

1.生成與自己關聯(lián)的Message

2.綁定到當前線程

主線程在初始化的時候已經(jīng)生成Looper,

其他線程如果想使用handler需要通過Looper.prepare()生成一個自己線程綁定的looper

這就是Looper.prepare()源碼,其實質(zhì)也是使用構(gòu)造器生成一個looper

private static void prepare(boolean quitAllowed) {

if (sThreadLocal.get() != null) {

throw new RuntimeException("Only one Looper may be created per thread");

}

sThreadLocal.set(new Looper(quitAllowed));

}

4.handler發(fā)送消息會將消息保存在自己相關聯(lián)的Looper的MessageQueue中,那它是如何找到這個MessageQueue的呢

public Handler(Callback callback, boolean async) {

if (FIND_POTENTIAL_LEAKS) {

final Class extends Handler> klass = getClass();

if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&

(klass.getModifiers() & Modifier.STATIC) == 0) {

Log.w(TAG, "The following Handler class should be static or leaks might occur: " +

klass.getCanonicalName());

}

}

mLooper = Looper.myLooper();

if (mLooper == null) {

throw new RuntimeException(

"Can't create handler inside thread that has not called Looper.prepare()");

}

mQueue = mLooper.mQueue;

mCallback = callback;

mAsynchronous = async;

}

這個是Handler的構(gòu)造方法,它會找到一個自己關聯(lián)的一個Looper

public static Looper myLooper() {

return sThreadLocal.get();

}

沒錯,他們之間也是通過線程關聯(lián)的,得到Looper之后自然就可以獲得它的MessageQueue了

5.我們再看下handler如發(fā)送消息,又是如何在發(fā)送完消息后,回調(diào)HandlerMessage的

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {

msg.target = this;

if (mAsynchronous) {

msg.setAsynchronous(true);

}

return queue.enqueueMessage(msg, uptimeMillis);

}

這個就是Handler發(fā)送消息的最終源碼,可見就是將一個message添加到MessageQueue中,那為什么發(fā)送完消息又能及時回調(diào)handleMessage方法呢

大家請看上邊那個loop方法,其中的for循環(huán)里面有一句話msg.target.dispatchMessage(msg);

public void dispatchMessage(Message msg) {

if (msg.callback != null) {

handleCallback(msg);

} else {

if (mCallback != null) {

if (mCallback.handleMessage(msg)) {

return;

}

}

handleMessage(msg);

}

}

這就是這句話,看到了吧里面會調(diào)用hanlerMessage,一切都聯(lián)系起來了吧

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

總結(jié)

以上是生活随笔為你收集整理的android 实例源码解释,Android Handler 原理分析及实例代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 五月天国产视频 | 青青草在线视频免费观看 | 欧美日韩综合一区二区 | 亚洲一区 欧美 | 中文字幕看片 | 亚洲精品国偷拍自产在线观看蜜桃 | 亚洲AV无码成人精品区东京热 | 精品久久久久久国产 | 国产精品久久毛片av大全日韩 | 无码国产69精品久久久久同性 | 国产亚洲精品电影 | 日韩怡红院 | 色多多黄色 | 国产一级性生活 | 爱爱视频网址 | 在线视频第一页 | 天天免费视频 | 精品在线观看免费 | 四虎永久在线精品免费一区二区 | 亚洲在线观看一区二区 | 亚洲成年 | 韩国三级免费 | 天堂中文在线视频 | 国产色图视频 | 日批在线视频 | 蜜臀av无码精品人妻色欲 | 日本久久久久久久久久久 | 免费黄色一级视频 | 国产一区二区三区精品在线观看 | 日本极品丰满ⅹxxxhd | 一级特黄妇女高潮2 | 我我色综合 | 一线毛片 | 亚洲第九页 | 正在播放木下凛凛xv99 | 成人国产精品免费观看 | 天天插夜夜爽 | 久久精品在线观看 | 精品美女 | 欧美一区二区三区在线视频 | 五月激情六月 | av中文网| 精品人妻无码一区二区三 | 91偷拍视频 | 日本一区二区三区中文字幕 | 久久成人激情 | 午夜视频一区二区三区 | 五月天激情丁香 | 亚洲天堂手机在线 | 日韩色婷婷| 久章操| 韩国av在线播放 | 五月天激情视频在线观看 | 91大神在线观看视频 | 国产精品国产三级国产传播 | 69国产在线| 蜜臀av性久久久久蜜臀aⅴ麻豆 | 日本精品久久 | 中文字幕精品一二三四五六七八 | 精品人妻av一区二区三区 | 亚洲97视频 | 亚洲综合在线第一页 | 色综合久| 国产老头户外野战xxxxx | 麻豆免费视频网站 | 97超碰站| 男人和女人日批视频 | 日韩精品无码一区二区三区久久久 | 日韩视频一 | 日日做夜夜爽毛片麻豆 | 日日干狠狠干 | 91久久综合精品国产丝袜蜜芽 | 日韩久久一区二区三区 | 香蕉视频一级片 | 无码粉嫩虎白一线天在线观看 | 超碰97成人 | 伊人91视频 | 欧美日韩激情在线观看 | 一本大道久久久久精品嫩草 | 国产一级片 | 色二区 | 中文字幕欧美另类精品亚洲 | 国产91啪| 在线色网站 | www.色国产 | 国产色在线| 日韩一级性生活片 | 男男免费视频 | www.成人在线 | 男女插鸡视频 | 老司机伊人 | 欧美片在线观看 | 色综合久久综合 | 国产成人精品综合久久久久99 | 日韩一级黄色 | 白丝校花扒腿让我c | 日韩视频在线一区二区 | 亚洲永久无码精品一区二区 | 久久国产黄色片 |