編輯:關於 Android 程式設計
Android 只允許在主線程(UI 線程)操作介面。因此當有耗時工作時,必須丟到子線程執行,再把結果「送回」主線程更新畫面——這正是 Handler 存在的意義。若主線程超過約 5 秒仍未回應,系統就會跳出「強制關閉」的對話框,所以這個機制並非可有可無的裝飾。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/description" />
</RelativeLayout>
子線程隨機挑一張圖,透過 Message 通知 Handler;Handler 可以同時處理多個子線程的訊息,用 what 欄位區分。
public class MainActivity extends Activity {
private static final int UPDATE_PIC = 0x100;
private static final int UPDATE_DATA = 0x101;
private ImageView imageView = null;
private MyHandler handler = null;
private UpdateThread updateThread = null;
private int[] path = new int[] {
R.drawable.psb1, R.drawable.psb2, R.drawable.psb3,
R.drawable.psb4, R.drawable.psb5, R.drawable.psb6 };
private String[] names;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
names = getResources().getStringArray(R.array.arrays);
imageView = (ImageView) findViewById(R.id.imageView);
handler = new MyHandler();
new Thread(new myThread()).start();
updateThread = new UpdateThread();
handler.post(updateThread);
}
private class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
if (msg.what == UPDATE_PIC) {
imageView.setImageResource(path[msg.arg1]);
setTitle(msg.getData().getString("name"));
} else if (msg.what == UPDATE_DATA) {
System.out.println("data = " + msg.arg1);
} else {
super.handleMessage(msg);
}
}
}
private class myThread implements Runnable {
@Override
public void run() {
int index = 0;
while (!Thread.currentThread().isInterrupted()) {
index = new Random().nextInt(path.length);
Message message = handler.obtainMessage();
message.arg1 = index; // 用 arg1/arg2 傳值,資源消耗最少
message.what = UPDATE_PIC;
Bundle bundle = new Bundle();
bundle.putString("name", names[index]);
message.setData(bundle);
handler.sendMessage(message);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private class UpdateThread implements Runnable {
int index = 0;
@Override
public void run() {
index++;
Message message = handler.obtainMessage();
message.arg1 = index;
message.what = UPDATE_DATA;
handler.sendMessage(message);
handler.postDelayed(updateThread, 3000);
}
}
@Override
protected void onDestroy() {
handler.removeCallbacks(updateThread);
super.onDestroy();
}
}
arg1、arg2 傳整數,資料量大時才用 Bundle(setData()),這樣對系統資源最友善。what 辨識來源與意圖。onDestroy() 呼叫 removeCallbacks(),避免 Activity 已銷毀卻仍收到延遲訊息而導致記憶體配置或崩潰。postDelayed() 與 sendMessageDelayed()。
ListView 中 CheckBox 錯位問題的原因與解決
本節引言 作為 ListView 的經典問題之一,如果你嘗試過自定義帶有 CheckBox 的 item,那麼當 item 數量超過一屏時,幾乎一定會遇到 CheckBo
Android 自定義 View 入門:屬性、測量與繪製流程
自定義 View 的基本流程 自定義 View 是 Android 開發進階的重要能力。一般流程包括定義自定義屬性、在程式碼中讀取屬性、可選地處理 onMeasure,最
Android 仿 UC 瀏覽器三點跳動載入動畫實作
本篇示範如何打造一個類似 UC 瀏覽器的「三點載入」動畫:三顆圓點以貝茲曲線的方式輪流跳動,並搭配透明度的漸變,營造出流暢的載入過場效果。以下依序拆解各個檔案。一、Mea
Android 下拉刷新與上拉加載:以 android-pulltorefresh 開源庫實作
為什麼需要它 App 呈現資訊時,不可能一次把全部資料拉回來。為了讓使用者能順手地「往下拉看更多、往下滑載入舊的」,動態分頁加載就成了標配。本文以開源專案 android