Handler使用方法

概述

本文讨论Handler的一些使用方法。

Java线程一文简单介绍了线程的使用方法。这是本文讨论的基础。

示例

background执行一个计算任务,然后需要UI上能够显示计算进展。

ui

代码中,Start之后,执行这个background任务。background任务会把计算进展情况通过Message知会给UI。——通信机制就是借助Handler对象。

布局文件 my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/the_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/progress" />

        <TextView
            android:id="@+id/progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hint" />

    </LinearLayout>

</LinearLayout>

字符串资源

<string name="start">Start</string>
<string name="progress">progress:</string>
<string name="hint">the current progress</string>

代码

下面省略掉自动生成的一些代码。

public class MainActivity extends Activity {

    protected static final String TAG = "MainActivity";
    private Button start = null;
    private TextView progress = null;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int count = msg.what;
            progress.setText("" + count);
        }
    };

    private Runnable adding = new Runnable() {

        @Override
        public void run() {
            double d;
            for (int count = 1; count <= 1000; count++) {
                for (int i = 0; i < 1000; i++) {
                    d = Math.sqrt(Math.sqrt(i));
                    Log.d(TAG, "count = " + count + ", sqrt(sqrt(" + i + "))=" + d);
                }
                if (count % 10 == 0) {
                    handler.sendEmptyMessage(count / 100);
                }
            }
        }

    };

    private void calculate() {
        Thread thread = new Thread(null, adding, "adding");
        thread.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        start = (Button) findViewById(R.id.start);
        progress = (TextView) findViewById(R.id.progress);

        start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                calculate();
            }

        });
    }

要点

如下几点:

  • 背景任务的线程:handler.sendEmptyMessage(int what);
  • Handle重载:public void handleMessage(Message msg)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值