Handler的基本使用(2)

主要内容:
1.Handler与线程
2.Bundle的用法

3.在新线程当中处理消息的方法

Handler与线程

在前面的文章中,直接使用handler.post方法来最终调用到线程的run函数,这个Handler和run函数是在同一个线程中运行的。可以通过下面的方法进行试验:

package com.example.handlertest2;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

	Handler handler = new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		System.out.println("Activityid=========>"+Thread.currentThread().getId());
		System.out.println("Activityname=========>" + Thread.currentThread().getName());
		handler.post(thread);
	}

	Runnable thread = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			System.out.println("Threadid=========>"+Thread.currentThread().getId());
			System.out.println("Threadname=========>" + Thread.currentThread().getName());			
		}
	};
	@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;
	}

}
方法是在activity和线程中加入了打印线程id和名字的相关信息。启动应用程序后,logcat显示如下:

从上面logcat可以看到,Activity和Thread是在同一个线程中运行的。

那么如何在新的线程中运行呢?

复写一个Thread

	Thread newThread = new Thread(){
		public void run() {
			System.out.println("NewThreadid=========>"+Thread.currentThread().getId());
			System.out.println("NewThreadname=========>" + Thread.currentThread().getName());			
		};
	};
在activity中调用start方法:

		Thread r = new Thread(newThread);
		r.start();
logcat如下:


Bundle的用法

Bundle可以看作特殊的map,数据存储的工具,在进行消息传递的时候使用。
在新线程当中处理消息的方法

package com.example.handlertest3;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		System.out.println("The activity id  is " +Thread.currentThread().getId());
		HandlerThread handlerThread = new HandlerThread("Handler_Thread");//创建一个HandlerThread对象
		handlerThread.start();
		MyHandler handler = new MyHandler(handlerThread.getLooper());//复写一个handler,传入HandlerThread的looper
		Message msg = handler.obtainMessage();
		msg.arg1 = 5;
		handler.sendMessage(msg);
	}

	class MyHandler extends Handler{
		public MyHandler(Looper looper) {
			// TODO Auto-generated constructor stub
			super(looper);
		}
		
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			int age = msg.arg1;
			System.out.println("The age is "+age);
			System.out.println("The Handler id  is " +Thread.currentThread().getId());
		}
	}
	
	@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;
	}

}
HandlerThread类本身实现了具有循环来处理消息队列的功能,创建对象后需要调用start进行启动新的线程

上面只是传入了一个整型值,如何传入复杂的值呢??

这就需要上面所说的bundle了。

bundle的使用:

Bundle bundle = new Bundle();
bundle.putInt("age", 27);
bundle.putString("name", "Shanl");
msg.setData(bundle);
msg.sendToTarget();;//消息发送到taget

接收:

Bundle bundle = msg.getData();
int age = bundle.getInt("age");
String name = bundle.getString("name");
System.out.println("name is ===>"+name + "age is ==> "+age);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值