haovin https://blog.csdn.net/haovin/article/details/89600054
一、AndroID中的HandlerThread
1.1主要用途
a、定时器,未来某时做某事
b、线程之间传递消息
c、嵌入式:apk 调用XHService 时,权限不够,由Handler 传递消息,可以获得 system jurisdiction
二、Handler的使用
2.1 定时器
package com.gatsby.handlerdelaytime;import androID.os.Bundle;import androID.os.Handler;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androIDx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity { button btn1,btn2; int count = 0; Handler handler = new Handler();//创建一个Handler对象: private Runnable runnable = new Runnable() { @OverrIDe public voID run() { Log.d("gatsby", "Thread.currentThread().getID()->"+Thread.currentThread().getID()); count++; Log.d("gatsby", "count=" + count); handler.postDelayed(runnable, 1000);//1秒后调用此Runnable对象 } }; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); btn1 = (button) this.findVIEwByID(R.ID.btn1); btn2 = (button) this.findVIEwByID(R.ID.btn2); btn1.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw arg0) { Thread thread = new Thread(runnable); thread.start(); } }); btn2.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw arg0) { handler.removeCallbacks(runnable);//关闭此定时器 } }); }}
2.2 使用方式
Handler的使用方式可以根据发送消息的方式不同而分为两种:
Handler.sendMessage()和Handler.post()
2.3 Ed Sheeran
方式一、使用Handler.sendMessage()
a、创建Handler子类
package com.gatsby.handlertest;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.TextVIEw;import androIDx.annotation.NonNull;import androIDx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener { TextVIEw tv1; button btn1; // 2、在主线程中创建自定义Handler(mHandler)的实例 private CrushHandler crushHandler = new CrushHandler(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); tv1 = (TextVIEw) findVIEwByID(R.ID.tv1); btn1 = (button) findVIEwByID(R.ID.btn1); btn1.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.btn1: MyRunnable(); break; } } public voID MyRunnable() { Runnable runnable = new Runnable() { @OverrIDe public voID run() { // 3、创建所需消息对象 Message msg = Message.obtain(); msg.what = 1; //4、发送消息 crushHandler.sendMessage(msg); } }; Thread thread = new Thread(runnable); thread.start(); } //1、自定义Handler子类,继承Handler,重写handleMessage()方法 class CrushHandler extends Handler { @OverrIDe public voID handleMessage(@NonNull Message msg) { super.handleMessage(msg); //执行的UI *** 作 tv1.setText("新建Handler子类方法"); } }}
创建Handler子类1、自定义Handler子类,并重写handleMessage()方法 class CrushHandler extends Handler { //重写handleMessage()方法 @OverrIDe public voID handleMessage(@NonNull Message msg) { super.handleMessage(msg); //执行的UI *** 作 tv1.setText("新建Handler子类方法"); } }2、主线程中创建mHandler的实例 private CrushHandler crushHandler = new CrushHandler();3、在子线程中创建需要发送的消息对象 Message msg = Message.obtain(); msg.what = 1;4、在子线程中通过Hander发送消息到消息队列 crushHandler.sendMessage(msg);5、启动子线程
b、使用匿名内部类
package com.gatsby.handlertest;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.TextVIEw;import androIDx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener { TextVIEw tv1; button btn1; //1、在主线程中通过匿名内部类创建Handler类对象 private Handler mHandler = new Handler(new Handler.Callback() { @OverrIDe public boolean handleMessage(Message msg) { //执行的UI *** 作 tv1.setText("匿名内部类方法"); return false; } }); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); tv1 = (TextVIEw) findVIEwByID(R.ID.tv1); btn1 = (button) findVIEwByID(R.ID.btn1); btn1.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.btn1: MyRunnable(); break; } } public voID MyRunnable() { Runnable runnable = new Runnable() { @OverrIDe public voID run() { // 2、创建所需消息对象 Message msg = Message.obtain(); msg.what = 1; //3、发送消息 mHandler.sendMessage(msg); } }; Thread thread = new Thread(runnable); thread.start();//4、开启线程 }}
方式二、post方法
post机制将runnable对象运行在主线程中的
package com.gatsby.handlertest;import androID.os.Bundle;import androID.os.Handler;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.TextVIEw;import androIDx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener { TextVIEw tv1; button btn1; //1、在主线程中创建Handler的实例 private Handler mHandlerPost = new Handler(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); tv1 = (TextVIEw) findVIEwByID(R.ID.tv1); btn1 = (button) findVIEwByID(R.ID.btn1); btn1.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.btn1: Thread thread = new CrushThread(); thread.start(); break; } } class CrushThread extends Thread { @OverrIDe public voID run() { Runnable runnable = new Runnable() { @OverrIDe public voID run() { tv1.setText("post方法"); } }; mHandlerPost.post(runnable); } }}
总结
以上是内存溢出为你收集整理的Andorid 多线程(三):Handler 消息传递机制全部内容,希望文章能够帮你解决Andorid 多线程(三):Handler 消息传递机制所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)