说明:html
要想在非主线程中,建立Handler对象,首先须要使用Looper类的prepare方法来初始化一个Looper对象,而后建立这个Handler对象,再使用Looper类的loop方法,启动looper,从消息队列里获取和处理消息 。java
public void run(){android
super.run();app
Looper.prepare();dom
//实例化一个Handler对象ide
//....................oop
Looper.loop();this
}spa
下面用一个Demo(图片随机展现)线程
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demohandler" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.demohandler.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<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=".MainActivity" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
package com.example.demohandler; import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity implements Runnable{ private ImageView iv ; private Handler handler; private int[] path= new int[]{R.drawable.img1,R.drawable.img3,R.drawable.img3,R.drawable.img4}; private String[] title = new String[]{"高清壁纸1","高清壁纸2","高清壁纸3","高清壁纸4"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView)findViewById(R.id.imageView1); Thread t = new Thread(this); t.start(); handler = new Handler(){ public void handleMessage(Message msg){ TextView tv = (TextView)findViewById(R.id.textView1); if(msg.what ==0x101){ tv.setText(msg.getData().getString("title")); iv.setImageResource(path[msg.arg1]); } super.handleMessage(msg); } }; } @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; } @Override public void run() { // TODO Auto-generated method stub int index =0; while(!Thread.currentThread().isInterrupted()){ index = new Random().nextInt(path.length);//产生一个随机数 Message m = handler.obtainMessage();//获取一个Message m.arg1 = index;//保存要显示广告图片的索引值 Bundle bundle = new Bundle();// m.what = 0x101;//设置消息标识 bundle.putString("title", title[index]);//保存标题 m.setData(bundle);//将Bundle对象保存到Message中 handler.sendMessage(m);//发送消息 try{ Thread.sleep(2000); }catch(InterruptedException e){ e.printStackTrace(); } } } }