Android Looper And Hander 机制剖析 - 01
来源:互联网 发布:苗疆真的有蛊吗 知乎 编辑:程序博客网 时间:2024/06/05 18:00
1.关于任务调度
Android 本质是运行在移动设备上的一个操作系统,操作系统一个非常重要且核心的功能就是任务调度。当任务多且耗时的时候,操作系统会把任务放到队列里面存储起来,然后使用一个处理器从队列里面读取任务,逐个进行处理。从软件架构的层次来说,这种机制不一定需要操作系统来支持,使用责任链模式即可模拟简单的任务调度(在我之前写的关于设计模式的文章中有讨论)。我们在做工作流相关的系统开发的过程中,往往最后一步审核通过后,我们写入一些数据到数据库任务表中,使用定时执行程序从任务表中读取数据进行处理,这也是一种任务调度的策略。当然,负责的任务调度(比如操作系统的任务调度)通常有优先级等等诸多其他因素会影响到任务执行的先后顺序。但是我们从简单的开始,一步一步进入到复杂的方面,这样能够更好地理解前因后果,对于整个的设计有更加深入的理解,正所谓,知其然,也要知其所以然。
2.任务调度演化
Handler的基本使用
1) MainActivity
import com.example.handler1.R;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private TextView textView = null; private Button start = null; private Button end = null; Handler handler = new Handler(); Runnable updateThread = new Runnable(){ public void run(){ textView.append("\nUpdateThread Runing..."); //延时1s后又将线程加入到线程队列中 handler.postDelayed(updateThread, 1000); } }; @Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = (TextView)findViewById(R.id.text_view); start = (Button)findViewById(R.id.start); start.setOnClickListener(new StartClickListener()); end = (Button)findViewById(R.id.end); end.setOnClickListener(new EndClickListener());}private class StartClickListener implements OnClickListener{ public void onClick(View v) { // TODO Auto-generated method stub //将线程接口立刻送到线程队列中 textView.setText(""); handler.post(updateThread); } } private class EndClickListener implements OnClickListener{ public void onClick(View v) { // TODO Auto-generated method stub //将接口从线程队列中移除 handler.removeCallbacks(updateThread); } }}2.layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text_view" android:layout_width="fill_parent" android:layout_height="200dip" android:text="hello_world" tools:context="com.example.handler1.MainActivity" /> <Button android:id="@+id/start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="start" /> <Button android:id="@+id/end" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="end" /></LinearLayout>3.manifest文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.handler1" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.menu.MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
4.运行效果
点击开始,任务进入队列,点击结束,任务不再进入队列。
5.总结与分析
我们使用Runnable对象的实例来完成Handler的任务,虽然我们使用的是Runnable,但是还是在主线程成,这时候我们依然不可以做耗时操作,否则仍然会引发ANR错误,但是我们在这里看到了Handler的基本思想。下一篇我们继续讨论Hnadler的机制,讨论如何在另一个线程中完成任务。
0 0
- Android Looper And Hander 机制剖析 - 01
- Android Looper And Hander 机制剖析 - 02
- Android Looper And Hander 机制剖析 - 03
- 详述Android Looper和Hander
- Android Hander and Message
- Android消息机制剖析—Looper篇
- 理解Android Hander、Looper、Message 、MessageQueue、HandlerThread
- Android Hander机制的理解
- hander机制
- Android 消息机制之 handler、messageQueue、looper深入剖析
- Hander looper messageQueue
- Android中的异步消息处理机制Hander
- 浅析Android消息处理机制--Handler And Looper
- android hander
- Hander Looper 学习
- android Looper Handler机制
- Android Looper机制简介
- Android Native Looper机制
- typedef用法小结
- unity 对象池技术
- Linux文件系统的简单操作 - df, du, ln
- 知行合一1
- AlphaBeta剪枝算法
- Android Looper And Hander 机制剖析 - 01
- nginx限制请求数ngx_http_limit_req_module模块
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- Android studio使用
- 数据库连接
- JSP学习笔记(四)---自定义标签
- leetcode 67: Add Binary
- android中Baseadapter的getItem和getItemId的作用和重写
- UVA 10765 (割点)