Android Looper And Hander 机制剖析 - 01

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);
        }
    };
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值