面试常客Handler详细解析(handler基础)(一)
Handler是什么?
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();;
}
}
<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();
}
- 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"
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();
}
- 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" />
</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>
面试常客Handler详细解析(Handler与Looper、MessageQueue的关系)(二)
Handler与Looper、MessageQueue的关系:
Android为什么要设计只能通过Handler机制更新UI呢?
最根本的目的就是解决多线程并发问题,
假设如果在一个activity当中,有多个线程去更新UI,并且都没有加锁机制,那么会产生什么样子的问题? –》 更新界面错乱
如果对更新UI的操作都进行加锁处理的话又会产生什么样子的问题?–》
性能下降
出于对以上问题的考虑,Android给我们提供了一套更新UI的机制,我们只需要遵循这样的机制就可以了,根本不用去关心多线程问题,所有的更新UI操作,都是在主线程的消息队列当中去轮询处理的。
Handler封装了消息的发送(主要包括消息发送给谁(一般是发送给handler自己))
Looper
内部包含一个消息队列也就是MessageQueue,所有的Handler发送的消息都走向这个消息队列
Looper.Looper方法,就是一个死循环,不断的从MessageQueue取消息,如果有消息就处理消息,没有消息就阻塞
MessageQueue,就是一个消息队列,可以添加消息,并处理消息
Handler也很简单,内部会跟Looper进行关联,也就是说在Handler的内部可以找Looper,找到Looper,也就找到了MessageQueue,在Handler中发送消息,其实是向MessageQueue队列中发送消息
总结:handler负责发送消息,looper负责接收handler发送的消息,并直接把消息回传给handler自己,MessageQueue就是一个存储消息的容器
Handler的原理是什么?
图解
我的Leader(其实是Looper对象) 我(也就是Handler对象)
( Handler的sendMessage的操作)
第一步: 我要上厕所
第二步: 去吧
(Looper.Looper操作,并把消息回传给我)
第三步:我去上厕所
(handler的handleMessage方法)
面试常客Handler详细解析(自定义与线程相关的Handler)(三)
2016年03月05日 17:09:50 小笨鸟的修炼之路 阅读数:214
自定义与线程相关的Handler:
下面我们来自定义一些与线程相关的handler,以巩固以上所学,就像上篇文章提到的那样,我们先用handler的sendMessage方法去发送消息,发送到looper以后,looper再将消息回传给handler,再使用handler的handleMessage去处理消息,在主线程中,这些过程都自动创建,我们无须干预,但是在子线程中,这些过程不会自动创建,我们需要手动创建,我们先发送消息,然后需要looper.prepare()准备好looper,在handler处理消息以后,使用looper.loop()去轮询,下面上验证通过的代码:
SecondActivity.java:
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;
public class SecondActivity extends Activity {
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
System.out.println("UI---------->"+Thread.currentThread());
};
};
class MyThread extends Thread{
public Handler handler;
@Override
public void run() {
Looper.prepare();
handler =new Handler(){
@Override
public void handleMessage(Message msg) {
System.out.println("currentThread:"+Thread.currentThread());
}
};
Looper.loop();
}
}
private MyThread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("hello handler");
setContentView(tv);
thread = new MyThread();
thread.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thread.handler.sendEmptyMessage(1);
handler.sendEmptyMessage(1);
}
}
面试常客Handler详细解析(HandlerThread是什么)(四)
2016年03月05日 17:46:20 小笨鸟的修炼之路 阅读数:205
下面的代码会报异常:
SecondActivity.java:
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;
public class SecondActivity extends Activity {
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
System.out.println("UI---------->"+Thread.currentThread());
};
};
class MyThread extends Thread{
public Handler handler;
public Looper looper;
@Override
public void run() {
Looper.prepare();
looper=Looper.myLooper();
handler =new Handler(){
@Override
public void handleMessage(Message msg) {
System.out.println("currentThread:"+Thread.currentThread());
}
};
Looper.loop();
}
}
private MyThread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("hello handler");
setContentView(tv);
thread = new MyThread();
thread.start();
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// thread.handler.sendEmptyMessage(1);
// handler.sendEmptyMessage(1);
handler = new Handler(thread.looper){
@Override
public void handleMessage(Message msg) {
System.out.println(msg);
}
};
handler.sendEmptyMessage(1);
}
}
之所以会报异常的原因是由于多线程并发引起的,我们在子线程中创建的与主线程的looper会出现冲突,而在主线程运行到handler时,handler内部的looper还没有创建,却要从子线程传入一个looper对象,这就是报空指针异常。
针对以上问题,Android提供了HandlerThread来保证handler的正常运行,不会出现空指针异常,HandlerThread内部实现了同步机制,所以可以有效解决以上问题:
贴出源码:
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.widget.TextView;
public class ThreeActivity extends Activity {
private TextView tv;
private HandlerThread thread;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
tv = new TextView(this);
tv.setText("handler Thread");
setContentView(tv);
thread = new HandlerThread("handler thread");
thread.start();
handler = new Handler(thread.getLooper()){
@Override
public void handleMessage(Message msg) {
System.out.println("current thread------>"+Thread.currentThread());
}
};
handler.sendEmptyMessage(1);
}
}
面试常客Handler详细解析(主线程与子线程信息交互)(五)
2016年03月05日 18:09:16 小笨鸟的修炼之路 阅读数:131
主要讲解了主线程和子线程的handler的消息发送:
下面直接上代码:
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FourActivity extends Activity implements OnClickListener{
//创建主线程的handler
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
Message message = new Message();
System.out.println("main Handler");
//向子线程发送消息
threadHandler.sendMessageDelayed(message, 1000);
};
};
private Handler threadHandler;
private Button btn1,btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.four);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
HandlerThread thread = new HandlerThread("handlerThread");
thread.start();
//创建子线程的handler
threadHandler = new Handler(thread.getLooper()){
@Override
public void handleMessage(Message msg) {
Message message = new Message();
System.out.println("thread Handler");
//向主线程发送消息
handler.sendMessageDelayed(message, 1000);
}
};
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
handler.sendEmptyMessage(1);
break;
case R.id.button2:
handler.removeMessages(1);;
break;
}
}
}
[Android]Can't create handler inside thread that has not called Looper.prepare()
都是因为在新开的线程中更新UI才出错,子线程中更新主线程UI需要用到Handler. 今天有出现如下错误,代码如下: send.setOnClickListener(new OnClickLi...
面试常客Handler详细解析(更新UI的几种方式)(六)
2016年03月07日 09:39:40 小笨鸟的修炼之路 阅读数:179
一共有:
UI主线程 activityd的runOnUiThread
handler post
handler sendMessage
view post
下面将我自己已经验证成功的代码贴出了,其实这些方法都是殊途同归,都是使用了handler,封装成message进行发送的:
主程序:
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
public class FiveActivity extends Activity {
private TextView tv;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
tv.setText("ok2");
};
};
private void handle1(){
handler.post(new Runnable() {
@Override
public void run() {
tv.setText("ok");
}
});
}
private void handle2(){
handler.sendEmptyMessage(1);
}
private void updateUI(){
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
tv.setText("ok3");
}
});
}
private void viewUI(){
tv.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
tv.setText("ok4");
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fiva);
tv = (TextView) findViewById(R.id.tv);
new Thread(){
public void run() {
try {
Thread.sleep(2000);
viewUI();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();;
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:textSize="40sp"/>
</LinearLayout>