Android 线程通信(Handler + Message + Looper) 4
来源:互联网 发布:22周排畸b超数据看男女 编辑:程序博客网 时间:2024/06/07 05:29
参考:
Android 线程通信(Handler + Message + Looper) 0 - 前言
android.os.HandlerThread
HandlerThread
是 Android
系统封装好的已绑定 Looper
对象的线程
下面学习 HandlerThread
类的组成和使用
主要内容:
- 构造器
run()
- 线程终止
- 示例程序
构造器
构造函数有两个:
public HandlerThread(String name)public HandlerThread(String name, int priority)
参数 name
表示线程名,参数 priority
表示线程优先级
优先级设置
默认优先级大小为 0
public HandlerThread(String name) { super(name); mPriority = Process.THREAD_PRIORITY_DEFAULT;}public static final int THREAD_PRIORITY_DEFAULT = 0;
在 Android
系统中的线程,优先级取值范围为 [-20, 19]
(-20
表示最高优先级,19
表示最低优先级)
/** * Set the priority of a thread, based on Linux priorities. * * @param tid The identifier of the thread/process to change. * @param priority A Linux priority level, from -20 for highest scheduling * priority to 19 for lowest scheduling priority. * * @throws IllegalArgumentException Throws IllegalArgumentException if * <var>tid</var> does not exist. * @throws SecurityException Throws SecurityException if your process does * not have permission to modify the given thread, or to use the given * priority. */public static final native void setThreadPriority(int tid, int priority) throws IllegalArgumentException, SecurityException;
run()
run()
HandlerThread
类已经在 run()
方法执行了 Looper.prepare()
方法和 Looper.loop()
方法:
@Overridepublic void run() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper = Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared(); Looper.loop(); mTid = -1;}
如果想要在中间执行一些操作,可以重写 onLooperPrepared()
方法:
/** * Call back method that can be explicitly overridden if needed to execute some * setup before Looper loops. */protected void onLooperPrepared() {}
线程终止
由于 Looper.loop()
是一个无限循环,其退出条件是消息队列已终止,必须及时调用方法 quit()
/** * Quits the handler thread's looper. * <p> * Causes the handler thread's looper to terminate without processing any * more messages in the message queue. * </p><p> * Any attempt to post messages to the queue after the looper is asked to quit will fail. * For example, the {@link Handler#sendMessage(Message)} method will return false. * </p><p class="note"> * Using this method may be unsafe because some messages may not be delivered * before the looper terminates. Consider using {@link #quitSafely} instead to ensure * that all pending work is completed in an orderly manner. * </p> * * @return True if the looper looper has been asked to quit or false if the * thread had not yet started running. * * @see #quitSafely */public boolean quit() { Looper looper = getLooper(); if (looper != null) { looper.quit(); return true; } return false;}
调用 quit()
方法会立即终止 Looper
对象,舍弃消息队列中的消息
如果想要先完成剩余的消息再退出,可以使用方法 quitSafely()
/** * Quits the handler thread's looper safely. * <p> * Causes the handler thread's looper to terminate as soon as all remaining messages * in the message queue that are already due to be delivered have been handled. * Pending delayed messages with due times in the future will not be delivered. * </p><p> * Any attempt to post messages to the queue after the looper is asked to quit will fail. * For example, the {@link Handler#sendMessage(Message)} method will return false. * </p><p> * If the thread has not been started or has finished (that is if * {@link #getLooper} returns null), then false is returned. * Otherwise the looper is asked to quit and true is returned. * </p> * * @return True if the looper looper has been asked to quit or false if the * thread had not yet started running. */public boolean quitSafely() { Looper looper = getLooper(); if (looper != null) { looper.quitSafely(); return true; } return false;}
示例程序
public class HandlerThreadActivity extends AppCompatActivity { private MyThread thread = new MyThread("zj"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_handler_thread); thread.start(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } Message msg = Message.obtain(thread.handler, 0, 1000); msg.sendToTarget(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } thread.quit(); } class MyThread extends HandlerThread { private static final String TAG = "MyThread"; public Handler handler; public MyThread(String name) { super(name); } @Override protected void onLooperPrepared() { super.onLooperPrepared(); handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); Log.e(TAG, "handleMessage: " + msg.arg1); } }; } }}
Note:创建子类继承 HandlerThread
后,仍旧需要调用 start()
方法
阅读全文
0 0
- Android 线程通信(Handler + Message + Looper) 4
- Handler、Message、Looper等Android线程通信
- Android 线程通信(Handler + Message + Looper) 0
- Android 线程通信(Handler + Message + Looper) 1
- Android 线程通信(Handler + Message + Looper) 2
- Android 线程通信(Handler + Message + Looper) 3
- android线程handler、message、looper
- Android线程通信,Handler+Looper+Message机制解析
- android线程管理四(Looper,Handler,Message)
- Android线程—Handler、Looper、Message、MessageQueue
- Android线程间通信机制——深入理解 Looper、Handler、Message
- Android Handler,Looper,Message
- Android - Looper / Handler / Message
- Android Handler Message Looper
- Android Handler 、 Looper 、Message
- Android 线程4件套 MessageQueue Message Looper Handler之Looper
- Android线程间通信机制(handler,looper)
- Android线程间通信机制(Handler Looper )
- UltraEdit出现“初始化FTP失败”解决记录
- 新手请看完整版web服务器的架设
- Alloyteam Conf 2017 前端大会
- 技术文章
- TCP/IP数据包结构详解
- Android 线程通信(Handler + Message + Looper) 4
- HttpPost
- CHAPTER 10-Introduction to Artifcial Neural Networks
- Spark的二次排序解决方案
- Date与LocalDateTime、LocalDate、LocalTime互转
- 丘成桐在CNCC会议的演讲全文
- 程序员,30岁,如何趁 AI 浪潮突破年薪天花板?
- 为什么Java开发人员都带眼镜 | 程序员搞笑段子合集
- ofbiz——上传工具类HttpRequestFileUpload修改优化