博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Handler Demo
阅读量:5770 次
发布时间:2019-06-18

本文共 6731 字,大约阅读时间需要 22 分钟。

主源文件:

package com.example.handlerdemo;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {    private Button mHandlerBtn;    private Button mUpdateUIBtn;    private Button mTransDataBtn;    private Button mRunBtn;    private Button mPostDelayButton;    private EditText mEdit;    private Handler mHandler;    private Handler mHandlerData;    private Handler mHandlerrun;    private Handler mPostDelayHandler;    private Message msg = new Message();    private RunThread TestThread;    private int cnt = 10;    class RunThread implements Runnable {        @Override        public void run() {            // TODO Auto-generated method stub            try {                while (true) {                    //send this message every 1 seconds                    Thread.sleep(1000);                    cnt++;                    Bundle b = new Bundle();                    b.putLong("cnt", cnt);                    Message mssg = new Message();                    mssg.setData(b);                    mHandlerrun.sendMessage(mssg);                }            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }    class HandlerRunMessage extends Handler {        @Override        public void handleMessage(Message msg) {            // TODO Auto-generated method stub            super.handleMessage(msg);            Bundle b = new Bundle();            b = msg.getData();            mEdit.setText("number : " +String.valueOf(b.getLong("cnt")));        }    }    class HandlerMessage extends Handler {        @Override        public void handleMessage(Message msg) {            // TODO Auto-generated method stub                        //update ui            switch (msg.what) {            case 0:                mEdit.setText("update ui ...");                Toast.makeText(MainActivity.this, "update ui",                        Toast.LENGTH_SHORT).show();                break;            case 1:                Toast.makeText(MainActivity.this, "msg : " + 1,                        Toast.LENGTH_SHORT).show();                System.out.println("send message ok ...");            default:                break;            }            super.handleMessage(msg);        }    }    class HandleDataMessage extends Handler {        @Override        public void handleMessage(Message msg) {            // TODO Auto-generated method stub            super.handleMessage(msg);            Bundle b = msg.getData();            mEdit.setText(b.getCharSequence("content"));        }    }        class PostDelayHandleMessage extends Handler{        @Override        public void handleMessage(Message msg) {            // TODO Auto-generated method stub            super.handleMessage(msg);                        switch(msg.what){            case 0:                Toast.makeText(MainActivity.this, "post delay successfully", Toast.LENGTH_SHORT).show();                break;            }                    }            }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                mHandler = new HandlerMessage();        mHandlerData = new HandleDataMessage();        mHandlerrun = new HandlerRunMessage();                mPostDelayHandler=new PostDelayHandleMessage();                        mPostDelayButton=(Button)findViewById(R.id.button5);        mPostDelayButton.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                final Message msg=new Message();                msg.what=0;                mPostDelayHandler.postDelayed(new Runnable(){                    @Override                    public void run() {                        // TODO Auto-generated method stub                        Toast.makeText(MainActivity.this, "under post ...", Toast.LENGTH_SHORT).show();                        mPostDelayHandler.sendMessage(msg);                                            }                                    }, 2000);            }                    });        mEdit = (EditText) findViewById(R.id.editText1);        TestThread = new RunThread();        new Thread(TestThread).start();        mRunBtn = (Button) findViewById(R.id.button4);        mRunBtn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub//                Bundle b = new Bundle();//                b.putLong("cnt", cnt);//                mHandlerrun.sendMessage(msg);            }        });        mTransDataBtn = (Button) findViewById(R.id.button3);        mTransDataBtn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                Bundle bd = new Bundle();                // bd.putCharSequence("content", "transfer data ok !");                bd.putString("content", "transfer data OK !");                msg.setData(bd);                mHandlerData.sendMessage(msg);            }        });        mUpdateUIBtn = (Button) findViewById(R.id.button2);        mUpdateUIBtn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                msg.what = 0;                mHandler.sendMessage(msg);            }        });        mHandlerBtn = (Button) findViewById(R.id.button1);        mHandlerBtn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                new Thread(new Runnable() {                    @Override                    public void run() {                        // TODO Auto-generated method stub                        msg.what = 1;                        mHandler.sendMessage(msg);                    }                }).start();            }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }}

xml文件:

利用Handler发送数据信息,更新UI,延时处理事务...

 

转载地址:http://ckiux.baihongyu.com/

你可能感兴趣的文章
MonoRail学习笔记十一:页面控件的填充和验证
查看>>
Linux下RocketMQ环境的配置
查看>>
DirectX Effects初探
查看>>
Linux的防火墙–Iptables
查看>>
CrazePony飞行器--相关资料网址
查看>>
我的世界游戏服务器搭建
查看>>
windows消息机制(MFC)
查看>>
关于优先级反转【转】
查看>>
linux中fork()函数详解【转】
查看>>
setting.xml配置文件
查看>>
mysql索引总结----mysql 索引类型以及创建
查看>>
SQL Server 2012 数据库镜像配置完整篇
查看>>
指针小问题
查看>>
思科2960交换机与Windows server 2012 实现LACP链路聚合
查看>>
【译】MySQL char、varchar的区别
查看>>
开源中国社区 iPhone 客户端项目学习笔记
查看>>
机器学习理论与实验2
查看>>
分布式存储系统-GlusterFs概述
查看>>
CentOS 小问题集锦(一)
查看>>
4_frame_b.html
查看>>