android基础知识——handler基础知识

1、什么是handler?

handler是android提供的,用来更新UI的一套机制。也是一套消息处理的机制,可以通过handler发送消息、处理消息。

2、为什么要使用handler?

android在设计的时候,就封装了一套消息创建、传递、处理机制,如果不遵循这样的机制,就没有办法更新UI,抛出异常:

only the original thread that created a view hierarchy can touch its views.

(仅原生线程更新UI)。

3、handler用法:

Handler.post(Runnable r)

示例代码:

通过handler,改变TextView中的文本信息

	private Handler handler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		textview = (TextView) findViewById(R.id.textview);
		new Thread() {
			public void run() {
				try {
					Thread.sleep(1000);
					handler.post(new Runnable() {
						@Override
						public void run() {
							// TODO Auto-generated method stub
							textview.setText("hello");
						}
					});

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

			};
		}.start();
	}



Handler.postDelayed(Runnable r, long delayMillis)
实例代码:
通过handler,每隔1秒改变imageview中的图片
	private TextView textview;

	private Handler handler = new Handler();

	private ImageView imageview;
	private int images[] = { R.drawable.image1, R.drawable.image2, R.drawable.image3 };
	private int index;
	private MyRunnable myRunnable = new MyRunnable();

	class MyRunnable implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			index++;
			index = index % 3;
			imageview.setImageResource(images[index]);
			handler.postDelayed(myRunnable, 1000);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		textview = (TextView) findViewById(R.id.textview);
		imageview = (ImageView) findViewById(R.id.imageview);
		handler.postDelayed(myRunnable, 1000);
	}


Handler.sendMessage(Message msg)
示例代码:
通过handler,改变TextView的文本信息
	private TextView textview;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			textview.setText("" + msg.arg1);
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		textview = (TextView) findViewById(R.id.textview);

		new Thread() {
			public void run() {
				try {
					Thread.sleep(2000);
					Message msg = new Message();
					msg.arg1 = 110;
					handler.sendMessage(msg);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			};
		}.start();
	}

上面的示例代码发送的是简单的参数,下面将发送较为复杂的。

	private TextView textview;

	class Person {
		private String name;
		private String age;

		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "name:" + name + "age:" + age;
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			textview.setText("" + msg.obj);
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		textview = (TextView) findViewById(R.id.textview);

		new Thread() {
			public void run() {
				try {
					Thread.sleep(2000);
					Person person = new Person();
					person.name = "姓名";
					person.age = "年龄";
					Message msg = new Message();
					msg.obj = person;
					handler.sendMessage(msg);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		}.start();
	}

Handler、Looper、MessageQueue的关系:
Handler负责发送消息,Looper负责接收Handler发送的消息,并直接把消息回传给Handler。
MessageQueue就是一个存储消息的容器
相关文章
相关标签/搜索