面试常客Handler详细解析(handler基础)(一)
2016-03-04 21:41
776 查看
Handler是什么?
handler是Android给我们提供用来更新UI的一套机制,也是一套消息处理的机制,我们可以用它发送消息,也可以通过它处理消息。
联系framework可以详细看到。生命周期的改变都是通过handler消息改变的。
为什么要用Handler?
不用handler更新UI是不行的,Android在设计的时候,就封装了一套消息创建、传递、处理机制,如果不遵循这样的机制,就没有办法更新UI信息的,就会抛出异常信息(不能在非UI线程中更新UI)。
子线程真的不能更新UI吗?
答案是否定的,下面看贴下自己已经运行通过的代码,用事实例证:
MainActivity.java:
对应的activity_main.xml:
运行结果:
事实证明,子线程是可以更新UI的,但是如果子线程先休眠一段时间再去更新就会失败,这个还得从ViewRootImp的创建说起,ViewRootImp是在onResume()中创建的,没有休眠的子线程不会经历onResume()阶段,只有休眠过的子线程才会经历onResume()阶段,才会创建ViewRootImp,才会不能让子线程更新UI。但是我们不建议这么做,我们最好不要在子线程中更新UI。
Handler怎么用?
handler更新UI:
既然不能在子线程中更新UI,那我们就只能使用handler来更新UI了,下面配合代码实现:
MainActivity.java:
activity_main.xml:
通过runnable轮换显示三张照片:
MainActivity.java:
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
// new Thread(){
// @Override
// public void run() {
// try {
// Thread.sleep(1000);
//// tv.setText(“hi”);
// handler.post(new Runnable() {
//
// @Override
// public void run() {
// tv.setText(“update…”);
//
// }// });
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }//
// }// }.start();
}对应的activity_main.xml:
发送处理消息:
MainActivity.java:
activity_main.xml:
传递复杂数据,比如自定义类,通过obj传递:
MainActivity.java:
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
//
// handler.postDelayed(runnable, 1000);
// new Thread(){
// @Override
// public void run() {
// try {
// Thread.sleep(1000);
//// tv.setText(“hi”);
// handler.post(new Runnable() {
//
// @Override
// public void run() {
// tv.setText(“update…”);
//
// }// });
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }//
// }// }.start();
}对应的布局文件activity_main.xml:
消息的移除,当button被按下后,轮询图片的runnable被移除:
对应的布局文件:
消息的截获:
通过生成handler时,带有一个callback接口,如果返回false,则不截获数据,如果返回true,则将数据截获,看下面的代码,查看按钮按下的toast信息就知道了:
MainActivity.java:
对应的布局文件activity_main.xml:
handler是Android给我们提供用来更新UI的一套机制,也是一套消息处理的机制,我们可以用它发送消息,也可以通过它处理消息。
联系framework可以详细看到。生命周期的改变都是通过handler消息改变的。
为什么要用Handler?
不用handler更新UI是不行的,Android在设计的时候,就封装了一套消息创建、传递、处理机制,如果不遵循这样的机制,就没有办法更新UI信息的,就会抛出异常信息(不能在非UI线程中更新UI)。
子线程真的不能更新UI吗?
答案是否定的,下面看贴下自己已经运行通过的代码,用事实例证:
MainActivity.java:
package com.example.handler; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView) findViewById(R.id.tv); new Thread(){ @Override public void run() { tv.setText("hi"); } }.start();; } }
对应的activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.handler.MainActivity" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
运行结果:
事实证明,子线程是可以更新UI的,但是如果子线程先休眠一段时间再去更新就会失败,这个还得从ViewRootImp的创建说起,ViewRootImp是在onResume()中创建的,没有休眠的子线程不会经历onResume()阶段,只有休眠过的子线程才会经历onResume()阶段,才会创建ViewRootImp,才会不能让子线程更新UI。但是我们不建议这么做,我们最好不要在子线程中更新UI。
Handler怎么用?
handler更新UI:
既然不能在子线程中更新UI,那我们就只能使用handler来更新UI了,下面配合代码实现:
MainActivity.java:
package com.example.handler; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView) findViewById(R.id.tv); new Thread(){ @Override public void run() { try { Thread.sleep(1000); // tv.setText("hi"); handler.post(new Runnable() { @Override public void run() { tv.setText("update..."); } }); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); } }
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.handler.MainActivity" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
通过runnable轮换显示三张照片:
MainActivity.java:
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv; private Handler handler = new Handler(); private ImageView imageView; private int[] images = {R.drawable.a,R.drawable.b,R.drawable.c}; private int index = 0; private MyRunnable runnable = new MyRunnable(); class MyRunnable implements Runnable{ @Override public void run() { index++; index=index%3; imageView.setImageResource(images[index]); handler.postDelayed(runnable, 1000); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); imageView = (ImageView) findViewById(R.id.imageView1); new Thread(runnable).start();//这两句话的作用是一样的 // handler.postDelayed(runnable, 1000);//这两句话的作用是一样的,用哪句都一样
// new Thread(){
// @Override
// public void run() {
// try {
// Thread.sleep(1000);
//// tv.setText(“hi”);
// handler.post(new Runnable() {
//
// @Override
// public void run() {
// tv.setText(“update…”);
//
// }// });
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }//
// }// }.start();
}
}对应的activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.handler.MainActivity" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/imageView1" android:layout_below="@+id/imageView1" android:layout_marginTop="25dp" android:text="hello" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="164dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
发送处理消息:
MainActivity.java:
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
tv.setText(""+msg.arg1);
};
};
private ImageView imageView;
private int[] images = {R.drawable.a,R.drawable.b,R.drawable.c};
private int index = 0;
private MyRunnable runnable = new MyRunnable();
class MyRunnable implements Runnable{
@Override
public void run() {
index++;
index=index%3;
imageView.setImageResource(images[index]);
handler.postDelayed(runnable, 1000);
}}@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
imageView = (ImageView) findViewById(R.id.imageView1);
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
Thread.sleep(2000);
Message message = new Message();
message.arg1 = 88;
handler.sendMessage(message);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}.start();
//
// new Thread(runnable).start();
// new Thread(){
// @Override
// public void run() {
// try {
// Thread.sleep(1000);
//// tv.setText("hi");
// handler.post(new Runnable() {
//
// @Override
// public void run() {
// tv.setText("update...");
//
// }// });
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }//
// }// }.start();
}}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.handler.MainActivity" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/imageView1" android:layout_below="@+id/imageView1" android:layout_marginTop="25dp" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="164dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
传递复杂数据,比如自定义类,通过obj传递:
MainActivity.java:
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
tv.setText(""+msg.obj);
};
};
private ImageView imageView;
private int[] images = {R.drawable.a,R.drawable.b,R.drawable.c};
private int index = 0;
private MyRunnable runnable = new MyRunnable();
class MyRunnable implements Runnable{
@Override
public void run() {
index++;
index=index%3;
imageView.setImageResource(images[index]);
handler.postDelayed(runnable, 1000);
}}class Person{
private int age;
private String name;
@Override
public String toString() {
// TODO Auto-generated method stub
return "name="+name+" age="+age;
}}@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
imageView = (ImageView) findViewById(R.id.imageView1);
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
Thread.sleep(2000);
Message message = new Message();
message.arg1 = 88;
message.arg2 = 100;
Person person = new Person();
person.age = 23;
person.name = "Joe";
message.obj= person;
handler.sendMessage(message);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}.start();
//
// handler.postDelayed(runnable, 1000);
// new Thread(){
// @Override
// public void run() {
// try {
// Thread.sleep(1000);
//// tv.setText(“hi”);
// handler.post(new Runnable() {
//
// @Override
// public void run() {
// tv.setText(“update…”);
//
// }// });
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }//
// }// }.start();
}
}对应的布局文件activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.handler.MainActivity" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/imageView1" android:layout_below="@+id/imageView1" android:layout_marginTop="25dp" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="164dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
消息的移除,当button被按下后,轮询图片的runnable被移除:
MainActivity.java:
package com.example.handler;
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.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private TextView tv;
private Button button;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
tv.setText(""+msg.obj);
};
};
private ImageView imageView;
private int[] images = {R.drawable.a,R.drawable.b,R.drawable.c};
private int index = 0;
private MyRunnable runnable = new MyRunnable();
//轮询显示三张图片
class MyRunnable implements Runnable{
@Override
public void run() {
index++;
index=index%3;
imageView.setImageResource(images[index]);
handler.postDelayed(runnable, 1000);
}}//自定义类,传递复杂数据类型使用
class Person{
private int age;
private String name;
@Override
public String toString() {
// TODO Auto-generated method stub
return "name="+name+" age="+age;
}}@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
tv = (TextView) findViewById(R.id.tv);
imageView = (ImageView) findViewById(R.id.imageView1);
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
Thread.sleep(2000);
// Message message = new Message();
// message.arg1 = 88;
// message.arg2 = 100;
Message message = handler.obtainMessage();
Person person = new Person();
person.age = 23;
person.name = "Joe";
message.obj= person;
// handler.sendMessage(message);
message.sendToTarget();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}.start();
//
handler.postDelayed(runnable, 1000);
// new Thread(){
// @Override
// public void run() {
// try {
// Thread.sleep(1000);
//// tv.setText("hi");
// handler.post(new Runnable() {
//
// @Override
// public void run() {
// tv.setText("update...");
//
// }// });
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }//
// }// }.start();
}@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.removeCallbacks(runnable);
}}
对应的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.handler.MainActivity" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/imageView1" android:layout_below="@+id/imageView1" android:layout_marginTop="25dp" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="164dp" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignRight="@+id/imageView1" android:text="Button" /> </RelativeLayout>
消息的截获:
通过生成handler时,带有一个callback接口,如果返回false,则不截获数据,如果返回true,则将数据截获,看下面的代码,查看按钮按下的toast信息就知道了:
MainActivity.java:
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private TextView tv;
private Button button;
private Handler handler = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ""+1, 1).show();
return true;
}}){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ""+2, 1).show();
}};
private ImageView imageView;
private int[] images = {R.drawable.a,R.drawable.b,R.drawable.c};
private int index = 0;
private MyRunnable runnable = new MyRunnable();
//轮询显示三张图片
class MyRunnable implements Runnable{
@Override
public void run() {
index++;
index=index%3;
imageView.setImageResource(images[index]);
handler.postDelayed(runnable, 1000);
}}//自定义类,传递复杂数据类型使用
class Person{
private int age;
private String name;
@Override
public String toString() {
// TODO Auto-generated method stub
return "name="+name+" age="+age;
}}@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
tv = (TextView) findViewById(R.id.tv);
imageView = (ImageView) findViewById(R.id.imageView1);
// new Thread(){
// @Override
// public void run() {
// // TODO Auto-generated method stub
// super.run();
// try {
// Thread.sleep(2000);
//// Message message = new Message();
//// message.arg1 = 88;
//// message.arg2 = 100;
// Message message = handler.obtainMessage();
// Person person = new Person();
// person.age = 23;
// person.name = "Joe";
// message.obj= person;
//// handler.sendMessage(message);
// message.sendToTarget();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }// }//
// }.start();
//
// handler.postDelayed(runnable, 1000);
// new Thread(){
// @Override
// public void run() {
// try {
// Thread.sleep(1000);
//// tv.setText("hi");
// handler.post(new Runnable() {
//
// @Override
// public void run() {
// tv.setText("update...");
//
// }// });
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }//
// }// }.start();
}@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// handler.removeCallbacks(runnable);
handler.sendEmptyMessage(1);
}}
对应的布局文件activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.handler.MainActivity" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/imageView1" android:layout_below="@+id/imageView1" android:layout_marginTop="25dp" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="164dp" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignRight="@+id/imageView1" android:text="Button" /> </RelativeLayout>
相关文章推荐
- 程序员每天自我洗脑和催眠
- 程序员的自我修养
- 在职场该保持一种什么姿态?高调还是低调
- 面试题集合
- Java面试之一+=赋值符号的使用
- 操作系统面试题
- 获取了职业资格证书二级/技师
- Java的Exception和Error面试题10问10答
- Java面试题多思路解析--有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中
- 面试
- 程序员必须知道的10大基础实用算法及其讲解
- 程序员的知识管理
- 最适合程序员加班吃的6大营养美食
- Java高级开发工程师面试考纲
- 字符编码常识及问题解析
- JAVA多线程和并发基础面试问答
- 百度前端实习生面试(连跪之旅)
- 求职这几天编写的TEST程序
- Java面试题之一自增
- 程序员与创业公司的那些事儿-----写在离职当天