handler和message传值两例

news/2024/7/7 5:01:07 标签: Android, message, Handler

程序效果:为了显示如何用message传值的简单例子

例1,点击按钮,持续显示当前系统时间(bundle传值,耗时,效率低)

例2,点击按钮,progressbar持续前进(message方法传值,效率高,但只能传整型int和对象object)

例1,主activity

[java]  view plain  copy
  1. package com.song;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.os.Message;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14.   
  15. public class C91_HandlerActivity extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.     TextView textview;  
  18.     Button button;  
  19.     MyThread mythread;  
  20.     Thread thread;  
  21.     MyHandler handler;  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.         textview=(TextView)findViewById(R.id.textview);  
  27.         button=(Button)findViewById(R.id.button);  
  28.         handler=new MyHandler();  
  29.         button.setOnClickListener(new OnClickListener() {  
  30.               
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 // TODO Auto-generated method stub  
  34.                 mythread=new MyThread();  
  35.                 thread=new Thread(mythread);  
  36.                 thread.start();  
  37.             }  
  38.         });  
  39.     }  
  40.     class MyHandler extends Handler  
  41.     {  
  42.         //接受message的信息  
  43.         @Override  
  44.         public void handleMessage(Message msg) {  
  45.             // TODO Auto-generated method stub  
  46.             super.handleMessage(msg);  
  47.             if(msg.what==1)  
  48.             {  
  49.                 textview.setText(msg.getData().getString("time"));  
  50.             }  
  51.               
  52.         }  
  53.     }  
  54.     class MyThread implements Runnable  
  55.     {  
  56.   
  57.         @Override  
  58.         public void run() {  
  59.             // TODO Auto-generated method stub  
  60.             while(true)  
  61.             {  
  62.                 try {  
  63.                     Thread.sleep(1000);  
  64.                     String time=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());  
  65.                     System.out.println(time);  
  66.                     Message message=new Message();  
  67.                     Bundle bundle=new Bundle();  
  68.                     bundle.putString("time", time);  
  69.                     message.setData(bundle);//bundle传值,耗时,效率低  
  70.                     handler.sendMessage(message);//发送message信息  
  71.                     message.what=1;//标志是哪个线程传数据  
  72.                     //message有四个传值方法,  
  73.                     //两个传int整型数据的方法message.arg1,message.arg2  
  74.                     //一个传对象数据的方法message.obj  
  75.                     //一个bandle传值方法  
  76.   
  77.                 } catch (InterruptedException e) {  
  78.                     // TODO Auto-generated catch block  
  79.                     e.printStackTrace();  
  80.                 }  
  81.             }  
  82.         }  
  83.           
  84.     }  
  85. }  
布局文件

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6. <Button  android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content"  
  8.         android:text="开始"  
  9.         android:id="@+id/button"/>  
  10.     <TextView  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"   
  13.         android:textSize="20dp"  
  14.         android:textStyle="bold"  
  15.         android:id="@+id/textview"/>  
  16.   
  17. </LinearLayout>  
例2,主activity

[java]  view plain  copy
  1. package com.song;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.os.Message;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.ProgressBar;  
  11.   
  12. public class C92_Handler2Activity extends Activity {  
  13.     /** Called when the activity is first created. */  
  14.     Button button;  
  15.     ProgressBar bar;  
  16.     MyThread mythread;  
  17.     Thread thread;  
  18.     MyHandler handler;  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.         button=(Button)findViewById(R.id.button);  
  24.         bar=(ProgressBar)findViewById(R.id.bar);  
  25.         handler=new MyHandler();  
  26.         button.setOnClickListener(new OnClickListener() {  
  27.               
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 // TODO Auto-generated method stub  
  31.                 mythread=new MyThread();  
  32.                 thread=new Thread(mythread);  
  33.                 thread.start();  
  34.             }  
  35.         });     
  36.     }   
  37.     class MyHandler extends Handler  
  38.     {  
  39.         @Override  
  40.         public void handleMessage(Message msg) {  
  41.             // TODO Auto-generated method stub  
  42.             super.handleMessage(msg);  
  43.             if(msg.what==1)  
  44.             {  
  45.                 System.out.println(msg.arg1+"handle");  
  46.                 bar.setProgress(msg.arg1);  
  47.             }  
  48.         }  
  49.     }  
  50.     
  51.     class MyThread implements Runnable  
  52.     {  
  53.         int pro=0;  
  54.         @Override  
  55.         public void run() {  
  56.             // TODO Auto-generated method stub  
  57.             while(true)  
  58.             {  
  59.                 try {  
  60.                     Thread.sleep(1000);  
  61.                     pro=bar.getProgress()+1;  
  62.                     bar.setProgress(pro);   
  63.                     System.out.println(pro+"thread");  
  64.                     Message message=new Message();  
  65.                     message.arg1=pro;  
  66.                     message.what=1;  
  67.                     handler.sendMessage(message);  
  68.                       
  69.                 } catch (InterruptedException e) {  
  70.                     // TODO Auto-generated catch block  
  71.                     e.printStackTrace();  
  72.                 }  
  73.                   
  74.             }  
  75.         }  
  76.           
  77.     }  
  78.      
  79. }  

布局文件

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6. <Button android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content"  
  8.         android:id="@+id/button"  
  9.         android:text="开始"  
  10.     />  
  11.     <ProgressBar android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:id="@+id/bar"  
  14.         style="@android:style/Widget.ProgressBar.Horizontal"  
  15.         />  
  16.   
  17. </LinearLayout>  

显示效果



http://www.niftyadmin.cn/n/829404.html

相关文章

数据库总结(二)

Android开发的童鞋应该都知道&#xff0c;使用官方的SQLite数据库&#xff0c;可以满足我们大部分增删改查的需求&#xff0c;然而随着Android技术的逐步成长&#xff0c;你会慢慢发现SQLite越来越不能满足我们的需求。总结为以下主要几点&#xff1a; 1、创表&#xff0c;增删…

GreenDao的基本使用

一,配置 1,在项目的gradle中添加插件 apply plugin: com.android.application apply plugin: org.greenrobot.greendao // greendao的插件android {compileSdkVersion 26buildToolsVersion "26.0.0"defaultConfig {}2,配置greendaogreendao {schemaVersion 1 …

java solr 查询条件_Solr复杂条件查询

solr复杂查询条件查询(排序、过滤、高亮)简单案例&#xff1a;package cn.kingdee;import java.util.List;import java.util.Map;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrQuery.ORDER;import org.apache.solr.client.solrj.So…

Android--httpclient模拟post请求和get请求

HttpClient的使用模式&#xff1a; 1. 创建一个HttpClent 2.实例化新的HTTP方法&#xff0c;比如PostMethod 或 GetMethod 3.设置HTTP参数名称/值 4.使用HttpClent执行HTTP调用 5.处理Http响应 import java.io.IOException; import java.io.InputStream; import java.net.URLEn…

为什么改用Kotlin

为什么我要改用Kotlin 2017年05月18日 08:40:19 阅读数&#xff1a;86584 标签&#xff1a; android android开发 Kotlin 语言 更多 个人分类&#xff1a; Android 写在前面的话&#xff0c;作为一个不熬夜的人&#xff0c;一觉醒来发现Kotlin成为了Android的官方语言&…

用kottlin实现adapter

优秀文章:一份关于 Java、Kotlin 与 Android 的学习笔记 一,实体类的编写,使用data关键字,就可以自动实现equals()、hashCode()、toString(),get(),set()方法 data class DateBean(var name : String ,val age :Int ,var money : Double,var isChecked : Boolean) 二,在Activ…

android --多线程下载

多线程下载文件的过程是&#xff1a; (1)首先获得下载文件的长度&#xff0c;然后设置本地文件的长度。 HttpURLConnection.getContentLength();//获取下载文件的长度 RandomAccessFile file new RandomAccessFile("QQSetup.exe","rwd"); file.setLength…

android--显式跳转和隐式跳转

区别如下&#xff1a; 显式启动Activity指的是在Intent内部直接声明要启动的activity所对应的class。 隐式启动Activity的intent到底发给哪个activity&#xff0c;需要进行三个匹配&#xff0c;一个是action&#xff0c; 一个是category&#xff0c;一个是data&#xff0c;可…