handler和message传值两例

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

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

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

例1,主activity

package com.song;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class C91_HandlerActivity extends Activity {
    /** Called when the activity is first created. */
	TextView textview;
	Button button;
	MyThread mythread;
	Thread thread;
	MyHandler handler;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textview=(TextView)findViewById(R.id.textview);
        button=(Button)findViewById(R.id.button);
        handler=new MyHandler();
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mythread=new MyThread();
				thread=new Thread(mythread);
				thread.start();
			}
		});
    }
    class MyHandler extends Handler
    {
    	//接受message的信息
    	@Override
    	public void handleMessage(Message msg) {
    		// TODO Auto-generated method stub
    		super.handleMessage(msg);
    		if(msg.what==1)
    		{
    			textview.setText(msg.getData().getString("time"));
    		}
    		
    	}
    }
    class MyThread implements Runnable
    {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			while(true)
			{
				try {
					Thread.sleep(1000);
					String time=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
					System.out.println(time);
					Message message=new Message();
					Bundle bundle=new Bundle();
					bundle.putString("time", time);
					message.setData(bundle);//bundle传值,耗时,效率低
					handler.sendMessage(message);//发送message信息
					message.what=1;//标志是哪个线程传数据
					//message有四个传值方法,
					//两个传int整型数据的方法message.arg1,message.arg2
                    //一个传对象数据的方法message.obj
					//一个bandle传值方法

				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
    	
    }
}
布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<Button  android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="开始"
        android:id="@+id/button"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:textSize="20dp"
        android:textStyle="bold"
        android:id="@+id/textview"/>

</LinearLayout>
例2,主activity

package com.song;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class C92_Handler2Activity extends Activity {
    /** Called when the activity is first created. */
	Button button;
	ProgressBar bar;
	MyThread mythread;
	Thread thread;
	MyHandler handler;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button=(Button)findViewById(R.id.button);
        bar=(ProgressBar)findViewById(R.id.bar);
        handler=new MyHandler();
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mythread=new MyThread();
		        thread=new Thread(mythread);
				thread.start();
			}
		});   
    } 
    class MyHandler extends Handler
    {
    	@Override
    	public void handleMessage(Message msg) {
    		// TODO Auto-generated method stub
    		super.handleMessage(msg);
    		if(msg.what==1)
    		{
    			System.out.println(msg.arg1+"handle");
    			bar.setProgress(msg.arg1);
    		}
    	}
    }
  
    class MyThread implements Runnable
    {
        int pro=0;
		@Override
		public void run() {
			// TODO Auto-generated method stub
			while(true)
			{
				try {
					Thread.sleep(1000);
					pro=bar.getProgress()+1;
					bar.setProgress(pro); 
					System.out.println(pro+"thread");
					Message message=new Message();
					message.arg1=pro;
					message.what=1;
					handler.sendMessage(message);
					
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
    	
    }
   
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="开始"
    />
    <ProgressBar android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        />

</LinearLayout>

显示效果




  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
是的,IntegrationFlows确实可以接收MessageHandler和ServiceBusTemplate。 在Spring Integration中,我们可以使用MessageHandler将消息发送到Azure Service Bus队列或主题。使用MessageHandler的好处是,它隐藏了与Azure Service Bus交互的细节,并提供了更高级别的抽象,从而使发送消息变得更加简单和方便。 下面是一个使用MessageHandler将消息发送到Azure Service Bus的示例: ```java @Bean public IntegrationFlow messageFlow(ServiceBusTemplate serviceBusTemplate) { return IntegrationFlows.from(() -> MessageBuilder.withPayload("Hello, World!").build(), e -> e.poller(p -> p.fixedDelay(1000))) .handle(AzureServiceBus.outboundAdapter(serviceBusTemplate).destination("my-queue")) .get(); } ``` 在这个例子中,我们使用AzureServiceBus.outboundAdapter()方法创建了一个Azure Service Bus的输出适配器,并将其添加到IntegrationFlow中。我们还使用destination()方法指定了要发送到的队列名称,这里是"my-queue"。 另一方面,ServiceBusTemplate是Azure Service Bus的Java客户端库的一部分,可以用于发送和接收消息,管理队列和主题等操作。我们可以将ServiceBusTemplate注入到Spring Integration的组件中,并在需要的时候使用它来发送或接收消息。 下面是一个使用ServiceBusTemplate将消息发送到Azure Service Bus的示例: ```java @Bean public IntegrationFlow messageFlow(ServiceBusTemplate serviceBusTemplate) { return IntegrationFlows.from(() -> MessageBuilder.withPayload("Hello, World!").build(), e -> e.poller(p -> p.fixedDelay(1000))) .handle(m -> { serviceBusTemplate.send("my-queue", m); System.out.println("Message sent: " + m.getPayload()); }) .get(); } ``` 在这个例子中,我们在IntegrationFlow中添加了一个处理器来处理消息。在处理器中,我们使用ServiceBusTemplate的send()方法将消息发送到"my-queue"队列,并在控制台输出消息内容。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值