編輯:Android 開發實例
這篇文章整理自學習過程中的筆記,目標很單純:讓同樣剛接觸 Android 的朋友,能把「執行緒之間怎麼溝通」這件事真正搞懂,之後在自己的專案裡就能得心應手。內容偏長,建議分幾次閱讀;裡頭收錄了好幾個可獨立運行的範例,因為它們彼此環環相扣,所以放在同一篇。
應用程式第一次啟動時,Android 會為它開一個 Linux 進程與一條主執行緒,預設所有元件都在這裡面跑,同時系統會為每個 App 配置獨立的 Linux 使用者。
Android 會盡量把正在運行的進程留在記憶體中,只有在資源吃緊時才著手回收,藉此保證使用者當前操作的畫面永遠有足夠資源即時回應。判定順序的依據是「進程裡有什麼元件、這些元件處於什麼狀態」,重要性由高到低分為五級:
startService() 啟動、且不屬於前兩類的 Service。它雖然對使用者不可見,卻承擔著使用者高度關注的工作,例如播放音樂、背景下載。只要前台與可見進程記憶體充足,系統不會回收它。onStop() 的 Activity。對使用者體驗沒有直接影響,會被放進 LRU(最近最少使用)清單,需要記憶體時優先回收。若 Activity 的生命週期處理得當,被關閉幾乎無感。評級時取該進程中元件的最高級別;另外,當一個進程被另一個進程依賴時,它的級別也會跟著提高——被服務方永遠不會比服務方還不重要。實務上的結論是:耗時工作請交給 Service,而不是自己開子進程,特別是這件事可能比 Activity 本身存活得更久(例如背景播放音樂、上傳相機拍下的照片)。同理,BroadcastReceiver 遇到費時任務時,也應該啟動 Service 而非另開執行緒。
主執行緒(Main Thread)負責處理所有與 UI 相關的事件:按鍵、觸控、繪圖,再把它們分派給對應元件,因此也被稱為 UI 執行緒。
開發 Android 必須守住一條鐵律:Android 的 UI 操作不是執行緒安全的,所有 UI 操作都必須在 UI 執行緒中進行。
如果無視這條規則,把網路請求、資料庫查詢這類耗時任務也塞進主執行緒,整個介面就會被阻塞——事件無法及時分派,畫面無法重繪,使用者看起來就像程式當掉了。更糟的情況是阻塞超過 5 秒,系統會跳出 ANR(Application Not Responding)對話框。
於是很多人會想:那我另開一條執行緒不就得了?方向完全正確,但結果只有兩種:如果你在新執行緒裡做的是背景服務之類的事,恭喜你成功;如果你順手在裡面更新了 UI,就會收到一個非常經典的例外。
版面配置:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview01"
android:text="異常測試" />
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15pt"
android:textColor="#FF0000"
android:layout_toRightOf="@id/myButton"
android:layout_alignTop="@id/myButton" />
</RelativeLayout>
Activity 程式碼,重點在按鈕裡開了一條新執行緒去改 TextView:
package xiaohang.zhimeng;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Activity01 extends Activity {
private Button myButton;
private TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.myButton);
myTextView = (TextView) findViewById(R.id.myTextView);
myButton.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
// 在子執行緒中嘗試更新 UI
myTextView.setText("張三");
}
}.start();
}
}
}
執行後會得到:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
意思很明白:建立 view 階層的那條執行緒,才有權碰它底下的 view。所有 UI 相關操作都必須回到 UI 執行緒,這個例外你之後還會遇到很多次。
既然不能在子執行緒更新 UI,那合理的做法是:在子執行緒把資料準備好,透過訊息交給 UI 執行緒去更新畫面;播放器的啟動與停止也同理。要做到這件事,得先認識三個物件。
每條執行緒通常各自附屬一個訊息佇列,用來存放透過 Handler 發布的訊息,可用 Looper.myQueue() 取得目前執行緒的佇列,取不到會拋出空指標例外。關鍵點:App 啟動時,系統預設只為 UI 執行緒建立好 MessageQueue;你自己開的子執行緒預設是沒有佇列的。
Handler 用來發布或處理訊息,也能處理 Runnable 實例。每個 Handler 都會綁定唯一的一條執行緒與該執行緒的訊息佇列;建立時預設綁到「建立它的那條執行緒」。也就是說,在 UI 執行緒建立的 Handler(例如 xh_Handler),發出的訊息只會進入 UI 執行緒的佇列,也只有它能處理。常用方法:
public final boolean sendMessage(Message msg):把訊息放到所屬佇列的尾端。要注意的是,放訊息的一端不會阻塞,處理的一端卻可能阻塞。public void handleMessage(Message msg):所屬執行緒透過這個方法接收並處理訊息,實務上通常繼承 Handler 並覆寫它。Looper 是 Handler 與訊息佇列之間的橋樑:元件把訊息交給 Looper,Looper 放進佇列,再把佇列中的訊息分派給各個 Handler 去 handleMessage()。
Looper.myLooper() 取得目前執行緒的 Looper,若無關聯則回傳 null(不會拋例外)。Looper.getMainLooper() 取得主執行緒的 Looper。主執行緒預設就有 Looper,自訂子執行緒則沒有。Looper.prepare()(系統會為它建立訊息佇列),再呼叫 Looper.loop() 進入訊息迴圈,之後就能發訊息、取訊息、處理訊息。其他執行緒要發訊息給它,前提是拿得到它的 Looper。這個範例有三顆按鈕:
版面配置(main.xml,使用 RelativeLayout):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg0">
<TextView
android:id="@+id/textview01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:text="@string/hello" />
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview01"
android:background="@drawable/newbtn" />
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15pt"
android:textColor="#FF0000"
android:layout_toRightOf="@id/myButton"
android:layout_alignTop="@id/myButton" />
<Button
android:id="@+id/myButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/myButton"
android:text="開始" />
<Button
android:id="@+id/myButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/myButton01"
android:layout_marginLeft="10px"
android:layout_alignTop="@id/myButton01"
android:text="停止" />
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15pt"
android:textColor="#385E0F"
android:layout_below="@id/myTextView"
android:layout_toRightOf="@id/myButton02"
android:layout_alignTop="@id/myButton02" />
</RelativeLayout>
按鈕的狀態換圖靠 selector 描述檔(newbtn.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/im1" />
<item android:state_focused="true" android:drawable="@drawable/im2" />
<item android:drawable="@drawable/im3" />
</selector>
音樂 Service 很單純,用 MediaPlayer 播放 R.raw.test:
package xiaohang.zhimeng;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicService extends Service {
private MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
player = MediaPlayer.create(this, R.raw.test); // 載入音樂
player.start(); // 開始播放
}
@Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
}
核心的 Activity02,示範子執行緒如何把資料交回 UI 執行緒:
package xiaohang.zhimeng;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Activity02 extends Activity {
private Button myButton; // 切換文字顯示
private Button myButton01; // 啟動音樂服務
private Button myButton02; // 停止音樂服務
private TextView myTextView, textView;
// 主執行緒的 Looper
Looper looper = Looper.myLooper();
// 在主執行緒建立的 Handler,因此與主執行緒的佇列關聯
MyHandler myHandler = new MyHandler(looper);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.myButton);
myButton01 = (Button) findViewById(R.id.myButton01);
myButton02 = (Button) findViewById(R.id.myButton02);
myTextView = (TextView) findViewById(R.id.myTextView);
textView = (TextView) findViewById(R.id.textview2);
myButton.setOnClickListener(new MyButtonListener());
myButton02.setOnClickListener(new MyButton02Listener());
myButton01.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
System.out.println("the thread id is " + Thread.currentThread().getId());
// 從資源取得音樂路徑並截出檔名
String tmusicName = getResources().getString(R.raw.music_name);
int b = tmusicName.lastIndexOf("/");
int e = tmusicName.lastIndexOf(".");
String musicName = tmusicName.substring(b + 1, e);
// obtainMessage() 取得的 Message 會關聯呼叫它的 Handler,
// 也就是 UI 執行緒的 myHandler,訊息因此進入主執行緒佇列
Message message = myHandler.obtainMessage();
message.obj = musicName;
message.arg1 = 2; // 用 arg1 區分訊息種類
message.sendToTarget();
startService(new Intent("xiaohang.zhimeng.Android.MUSIC"));
}
}.start();
}
});
}
class MyButton02Listener implements OnClickListener {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
stopService(new Intent("xiaohang.zhimeng.Android.MUSIC"));
}
}.start();
}
}
// 第一顆按鈕:顯示或清空 myTextView
class MyButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
String str = myTextView.getText().toString();
if (!str.trim().equals("")) {
myTextView.setText("");
} else {
Runnable r = new Runnable() {
public void run() {
Message message = myHandler.obtainMessage();
message.arg1 = 1;
message.obj = "android小子";
message.sendToTarget();
}
};
new Thread(r).start();
}
}
}
// 繼承 Handler 並覆寫 handleMessage,處理送到 UI 執行緒佇列的訊息
class MyHandler extends Handler {
public MyHandler() {}
public MyHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
if (msg.arg1 == 1) {
myTextView.setText((CharSequence) msg.obj);
} else if (msg.arg1 == 2) {
textView.setText((CharSequence) msg.obj);
}
}
}
}
原理其實很樸素:按鈕有三種狀態——預設、按下、取得焦點(實務上也常把放開視為回到預設),只要在不同狀態下引用不同的圖片資源即可。做法是在 res/drawable 目錄放一個 selector XML,用 android:state_pressed、android:state_focused 等屬性搭配 android:drawable,然後在版面配置中以 android:background="@drawable/newbtn" 或 android:src 引用。順序有講究:具名的狀態item 要放在無條件的預設項目之前,否則永遠不會被命中。
selector 的完整屬性清單可參考官方 Drawable 文件:https://idunnolol.com/android/drawables.html#selector
Async 是 asynchronous、Task 是任務,合起來就是「異步任務」。它同樣是用來處理執行緒的,問題是:它跟我們上面寫的 Handler + Looper + MessageQueue 那套,差在哪裡?
實務上更新 UI 多半直接用 AsyncTask,而不是 Thread + Handler。AsyncTask 從 Android 1.5 起被放進 android.os 套件(更早的 1.0/1.1 SDK 叫做 UserTask),底層是 JDK 1.5 起提供的 concurrency 庫。做過 J2EE 的人都知道並發庫比原生 Thread 靈活強大得多;而 Thread 是 Java 早期為多執行緒設計的,比較陽春,同步與執行緒池很多东西得自己實作。Google 為了 Android 的 UI 刷新機制引入 Handler 與 Looper,兩者都基於訊息,一旦佇列阻塞或其他因素介入,就不那麼好使。
因此官方與社群普遍建議:能用 AsyncTask 就用 AsyncTask,呼叫更簡單、實測更可靠,也更好維護。Google 自家的 Browser 就大量用異步任務處理下載檔案、讀寫資料庫這類耗時 I/O。缺點當然也有:一旦 doInBackground() 開跑,就沒有辦法再對它發訊息,只能靠預先設定好的標記位來控制流程;某些需要高度彈性調度的場景,Thread + Handler + Looper 反而更合適。
AsyncTask。onPreExecute():執行前的準備工作。doInBackground(Params...):背景處理,可呼叫 publishProgress() 更新進度。onProgressUpdate(Progress...):publishProgress() 被呼叫後,由 UI 執行緒觸發,用來更新進度條等介面。onPostExecute(Result):任務完成後,把結果送回 UI 執行緒。這四個方法都不能手動呼叫。除 doInBackground() 之外,其餘三個都由 UI 執行緒呼叫,因此有兩條硬性要求:
execute() 必須在 UI 執行緒中呼叫。另外兩點容易踩雷:同一個 task 只能執行一次,重複呼叫會出現例外;而且無法手動中止,是否符合需求要先想清楚。
類別定義本身就是泛型:
public abstract class AsyncTask<Params, Progress, Result> {}
Params:對應 doInBackground(Params...) 的參數型別,也就是 execute(Params... params) 傳進來的東西,可以 execute(data) 也可以 execute(data1, data2, data3)。Progress:對應 onProgressUpdate(Progress...) 的參數型別。Result:對應 onPostExecute(Result) 的參數型別。Void(注意是大寫 V 的 Void,不是 void)。execute() 的原始碼值得一看,它順便示範了狀態檢查:
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
if (mStatus != Status.PENDING) {
switch (mStatus) {
case RUNNING:
throw new IllegalStateException("Cannot execute task:"
+ " the task is already running.");
case FINISHED:
throw new IllegalStateException("Cannot execute task:"
+ " the task has already been executed "
+ "(a task can be executed only once)");
}
}
mStatus = Status.RUNNING;
onPreExecute();
mWorker.mParams = params;
sExecutor.execute(mFuture);
return this;
}
可以看到 onPreExecute() 正是在這裡被觸發,整個流程由此開始。執行順序固定為:onPreExecute → doInBackground →(publishProgress → onProgressUpdate,可重複)→ onPostExecute,這段順序務必記牢。
功能與上一個範例幾乎相同,差異有兩點:其一,這次「清空文字」的動作也會走一次 AsyncTask;其二,按下停止鈕後,歌名 TextView 也會被清空。版面配置沿用同一份 main.xml,因此只列兩個類。
package xiaohang.zhimeng;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicService extends Service {
private MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
player = MediaPlayer.create(this, R.raw.test);
player.start();
}
@Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
}
package xiaohang.zhimeng;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Activity02 extends Activity {
public static String ARGS1;
private Button myButton, myButton01, myButton02;
private TextView myTextView, textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView = (TextView) findViewById(R.id.myTextView);
textView = (TextView) findViewById(R.id.textview2);
myButton = (Button) findViewById(R.id.myButton);
myButton01 = (Button) findViewById(R.id.myButton01);
myButton02 = (Button) findViewById(R.id.myButton02);
myButton.setOnClickListener(new MyButtonListener());
myButton02.setOnClickListener(new MyButton02Listener());
myButton01.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Activity02.ARGS1 = "2";
new task().execute(Activity02.ARGS1);
}
});
}
class MyButton02Listener implements OnClickListener {
@Override
public void onClick(View v) {
Activity02.ARGS1 = "3";
new task().execute(Activity02.ARGS1);
}
}
public class MyButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
Activity02.ARGS1 = "1";
new task().execute(Activity02.ARGS1);
}
}
private class task extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
System.out.println("the thread ID is " + Thread.currentThread().getId());
if ("1".equals(params[0])) {
return params[0];
} else if ("2".equals(params[0])) {
startService(new Intent("xiaohang.zhimeng.Android.MUSIC"));
return params[0];
} else if ("3".equals(params[0])) {
stopService(new Intent("xiaohang.zhimeng.Android.MUSIC"));
return params[0];
}
return null;
}
@Override
protected void onPostExecute(String result) {
String str = myTextView.getText().toString();
if (!str.trim().equals("")) {
myTextView.setText("");
return;
}
if ("1".equals(result)) {
myTextView.setText("android小子");
} else if ("2".equals(result)) {
String tmusicName = getResources().getString(R.raw.test);
int b = tmusicName.lastIndexOf("/");
int e = tmusicName.lastIndexOf(".");
textView.setText(tmusicName.substring(b + 1, e));
} else if ("3".equals(result)) {
textView.setText("");
}
}
}
}
程式中的 System.out.println 是刻意留下的,用來觀察各段程式究竟跑在哪條執行緒上,實際使用時自行清掉即可。
這個範例把四個回呼都用上了:doInBackground 產生數字並透過 publishProgress() 更新介面,任務結束後再用 Handler 延遲觸發下一輪,總共跑五次。由於 Android 規定異步任務執行完即結束,要重跑必須 new 一個新的實例。
package xiaohang.zhimeng;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.widget.TextView;
import android.widget.Toast;
public class testAsync extends Activity {
private static final int MSG_TIMER = 10000;
private TextView vText = null;
private static int times = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
vText = (TextView) findViewById(R.id.TextView01);
vText.setText("Num...");
new task().execute("->");
}
// 收到訊息後觸發新一輪任務
private final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG_TIMER:
new task().execute("->");
break;
}
}
};
private class task extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String pre = params[0];
for (int i = 0; i < 5; i++) {
publishProgress(pre + i); // 只是一則訊息,發完立刻返回
SystemClock.sleep(1000);
}
return "任務結束";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(testAsync.this, result, Toast.LENGTH_SHORT).show();
if (times > 5) {
return;
}
Message msg = Message.obtain();
msg.what = MSG_TIMER;
handler.sendMessageDelayed(msg, 10000L);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(testAsync.this, "開始執行第" + times + "次任務", Toast.LENGTH_SHORT).show();
times++;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
vText.append(values[0]);
}
}
}
如果看著幾個方法的參數覺得頭暈,建議先補一下泛型的基礎,之後寫 AsyncTask 會順很多。
子執行緒與主執行緒之間除了用 Message.obj,也可以透過 Bundle 攜帶多組資料。這個範例另外示範了 HandlerThread——它繼承自 Thread,已經幫你把 Looper 與訊息佇列的機制包好了,是官方提供的便利工具類。
package mars.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
public class HandlerTest2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("Activity-->" + Thread.currentThread().getId());
// HandlerThread 繼承自 Thread,內部已完成 Looper 與訊息佇列的處理
HandlerThread handlerThread = new HandlerThread("handler_thread");
// 呼叫 getLooper() 之前必須先 start()
handlerThread.start();
MyHandler myHandler = new MyHandler(handlerThread.getLooper());
Message msg = myHandler.obtainMessage();
Bundle b = new Bundle();
b.putInt("age", 20);
b.putString("name", "Jhon");
msg.setData(b);
msg.sendToTarget(); // 目標就是產生這個 msg 的 Handler
}
class MyHandler extends Handler {
public MyHandler() {}
public MyHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
Bundle b = msg.getData();
System.out.println("age is " + b.getInt("age") + ", name is " + b.getString("name"));
System.out.println("Handler--->" + Thread.currentThread().getId());
}
}
}
版面配置很單純,就是一個 TextView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
以下是我當時參考過的幾篇文章,寫得都不錯,值得搭配練習:
範例下載(線程交互的兩個範例含資源檔,檔案較大):
所有範例的測試環境為 Android 2.0(API Level 5)。下載連結可能已失效,僅供參考;與其依賴他人打包的檔案,不如照著範例自己敲一遍,效果會好得多。
最後一句話總結這篇長文:UI 只能在 UI 執行緒碰,耗時任務丟到背景,兩邊靠 Handler + Message 溝通;如果只是單純的「跑一段背景工作再回來更新畫面」,直接用 AsyncTask 就好。
Android MediaPlayer 範例:建立可控制音訊的播放器
這個範例會建立一個可播放、暫停、快轉與快退的音訊播放器。畫面中會包含播放控制按鈕、時間顯示與 SeekBar 進度條。 使用 raw 資源 將 song.mp3 放入 r
Android 登入畫面與錯誤嘗試管理
登入畫面是幾乎每個需要帳號服務的 App 必備的一環。使用者在此輸入帳號與密碼,系統驗證通過後才允許進入——從 Facebook、微博等社群平台,到各種自製 App 皆然
Android 網絡連接處理:URL 連接、HttpClient 與 WebView 三種方案
Android 提供多種網絡編程方式,本文逐一說明各自的適用場景與關鍵設定。 方案一:URL + URLConnection/HttpURLConnection jav
Android Virtual Device 建立與啟動教學
AVD 是什麼 Android Virtual Device 是 Android 模擬器的虛擬裝置設定。每個 AVD 可指定系統版本、螢幕配置與硬體參數,讓開發者模擬不同