什么时候需要使用Handler? 能举个例子吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 例如一个线程内想更新UI 但是非UI线程是不能更新UI的否则会报错,这时就需要Handler发送消息给主线程去更新UI了。 关于为什么需要使用Handler,可以参考一下这个例子线程和UI线程的通信package com.proper;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 HandlerActivity extends Activity { /** Called when the activity is first created. */ private Button start; private ProgressBar bar=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.out.println("点击按钮前"); setContentView(R.layout.main); start=(Button)findViewById(R.id.start); bar=(ProgressBar)findViewById(R.id.bar); start.setText("start"); start.setOnClickListener(new StartButtonListener()); } class StartButtonListener implements OnClickListener{ public void onClick(View v) { bar.setVisibility(View.VISIBLE); //将线程对象压进队列,立刻执行Rannable中的run方法。 handler.post(updateThread); // TODO Auto-generated method stub System.out.println("--------------1当前线程Id"+Thread.currentThread().getId()); System.out.println("--------------1当前线程名字"+Thread.currentThread().getName()); } } Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub System.out.println("--------------3当前线程Id"+Thread.currentThread().getId()); System.out.println("--------------3当前线程名字"+Thread.currentThread().getName()); bar.setProgress(msg.arg1); System.out.println("~~~~~~~~~~~~~~~~~~"+msg.arg1); //将线程对象压进队列,立刻执行Rannable中的run方法。 handler.post(updateThread); if(msg.arg1==50) { System.out.println("!!!!!!!!!!!^^^^^!!!!!!!!!!!!"); handler.removeCallbacks(updateThread); System.out.println("****************************"); } } }; Runnable updateThread=new Runnable() { int i=0; public void run() { // TODO Auto-generated method stub System.out.println("--------------2当前线程Id"+Thread.currentThread().getId()); System.out.println("--------------2当前线程名字"+Thread.currentThread().getName()); i=i+10; //得到一个消息对象 Message msg=handler.obtainMessage(); msg.arg1=i; try{ Thread.sleep(1000); } catch(InterruptedException e){e.printStackTrace();} //将消息压进消息队列里面,然后异步执行Handle中的handleMessage方法 handler.sendMessage(msg); } };}<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ProgressBar android:layout_width="200dp" android:id="@+id/bar" style="?android:attr/progressBarStyleHorizontal" android:visibility="gone" android:layout_height="wrap_content"/> <Button android:id="@+id/start" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout> android程序第一安装,安装完成点击打开,进入应用,在推到通知栏,点击通知不只能进入登录界面 android游戏的来电中断如何处理 android spinner 文字不能居左 webview使用问题,谢谢大家 android 仿真系统安装apk文件 布局问题新手 android 截取短信的问题 准备把安卓APP外包出去,需要注意哪些呢?求教 FATAL EXCEPTION:GLTHREAD11 java.lang.nullpointerexception 新手求助,sdk下载的插件用不了是什么原因 xml添加了发送短信权限,还是不能发送短信 菜鸟在示例基础上增加功能,怎样组织架构?
线程和UI线程的通信package com.proper;
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 HandlerActivity extends Activity {
/** Called when the activity is first created. */
private Button start;
private ProgressBar bar=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("点击按钮前");
setContentView(R.layout.main);
start=(Button)findViewById(R.id.start);
bar=(ProgressBar)findViewById(R.id.bar);
start.setText("start");
start.setOnClickListener(new StartButtonListener());
}
class StartButtonListener implements OnClickListener{ public void onClick(View v) {
bar.setVisibility(View.VISIBLE);
//将线程对象压进队列,立刻执行Rannable中的run方法。
handler.post(updateThread);
// TODO Auto-generated method stub
System.out.println("--------------1当前线程Id"+Thread.currentThread().getId());
System.out.println("--------------1当前线程名字"+Thread.currentThread().getName());
}
}
Handler handler=new Handler(){ @Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
System.out.println("--------------3当前线程Id"+Thread.currentThread().getId());
System.out.println("--------------3当前线程名字"+Thread.currentThread().getName());
bar.setProgress(msg.arg1);
System.out.println("~~~~~~~~~~~~~~~~~~"+msg.arg1); //将线程对象压进队列,立刻执行Rannable中的run方法。
handler.post(updateThread);
if(msg.arg1==50)
{
System.out.println("!!!!!!!!!!!^^^^^!!!!!!!!!!!!");
handler.removeCallbacks(updateThread);
System.out.println("****************************");
}
}
};
Runnable updateThread=new Runnable() {
int i=0;
public void run() {
// TODO Auto-generated method stub
System.out.println("--------------2当前线程Id"+Thread.currentThread().getId());
System.out.println("--------------2当前线程名字"+Thread.currentThread().getName());
i=i+10;
//得到一个消息对象
Message msg=handler.obtainMessage();
msg.arg1=i;
try{
Thread.sleep(1000);
}
catch(InterruptedException e){e.printStackTrace();}
//将消息压进消息队列里面,然后异步执行Handle中的handleMessage方法
handler.sendMessage(msg);
}
};
}<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ProgressBar
android:layout_width="200dp"
android:id="@+id/bar"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>