說明: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(); } } } }