編輯:開發入門
Intent 是 Android 特有的元件溝通機制。你可以在 Intent 中指定程式要執行的動作(例如 view、edit、dial)以及該動作所需的資料,指定好之後只要呼叫 startActivity(),系統便會自動找出最符合條件的應用程式並執行它。以下把實務上常用到的寫法整理成一份清單。
BroadcastReceiver 沒有 Activity 的任務堆疊,因此從它啟動新的 Activity 時,必須明確加上 FLAG_ACTIVITY_NEW_TASK,否則會直接拋出例外:
public class MyReceiver extends BroadcastReceiver {
public static final String action = "acc";
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, Receivered.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
一、只指定 action 與 type
// SIM 匯入
Intent importIntent = new Intent(Intent.ACTION_VIEW);
importIntent.setType("vnd.android.cursor.item/sim-contact");
importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
menu.add(0, 0, 0, R.string.importFromSim)
.setIcon(R.drawable.ic_menu_import_contact)
.setIntent(importIntent);
二、指定 action、data 與 type(隱式查找)
// uri: content://simcontacts/simPeople/(id)
Intent intent = new Intent("android.intent.action.SIMEDIT", uri);
startActivity(intent);
程式會依 data 中的 uri 去尋找對應的 type,這一步由 ContentProvider 的 getType() 提供:
case SIM_PEOPLE_ID:
return "vnd.android.cursor.item/sim-contact";
並在 AndroidManifest.xml 中以 intent-filter 比對:
<intent-filter>
<action android:name="android.intent.action.SIMEDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/sim-contact" />
</intent-filter>
若想自己指定 type,只能使用 setDataAndType(),因為 data 與 type 必須同時設定才不會互相覆蓋。
三、其他設定 Intent 屬性的方法
Intent setComponent(ComponentName component) Intent setClassName(Context packageContext, String className) Intent setClassName(String packageName, String className) Intent setClass(Context packageContext, Class cls)
Uri uri = Uri.parse("https://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
要做路線規劃,則把起訖座標組進 Google Maps 的網址再交給 ACTION_VIEW 即可。
// 只開啟撥號面板,由使用者按下通話鍵
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
// 直接撥出,必須在 manifest 加入 CALL_PHONE 權限
Uri uri2 = Uri.parse("tel:xxxxxx");
Intent it2 = new Intent(Intent.ACTION_CALL, uri2);
startActivity(it2);
// 開啟簡訊撰寫畫面
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
// 指定收件者
Uri uri = Uri.parse("smsto:0800000123");
Intent it2 = new Intent(Intent.ACTION_SENDTO, uri);
it2.putExtra("sms_body", "The SMS text");
startActivity(it2);
// 附加圖片(MMS)
Uri uri3 = Uri.parse("content://media/external/images/media/23");
Intent it3 = new Intent(Intent.ACTION_SEND);
it3.putExtra("sms_body", "some text");
it3.putExtra(Intent.EXTRA_STREAM, uri3);
it3.setType("image/png");
startActivity(it3);
// 最簡單的方式
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
// 帶入主旨與本文,並讓使用者挑選郵件程式
Intent it2 = new Intent(Intent.ACTION_SEND);
it2.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
it2.putExtra(Intent.EXTRA_TEXT, "The email body text");
it2.setType("text/plain");
startActivity(Intent.createChooser(it2, "Choose Email Client"));
// 多收件者、副本、主旨
Intent it3 = new Intent(Intent.ACTION_SEND);
String[] tos = {"me@abc.com"};
String[] ccs = {"you@abc.com"};
it3.putExtra(Intent.EXTRA_EMAIL, tos);
it3.putExtra(Intent.EXTRA_CC, ccs);
it3.putExtra(Intent.EXTRA_TEXT, "The email body text");
it3.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it3.setType("message/rfc822");
startActivity(Intent.createChooser(it3, "Choose Email Client"));
// 加上附件
Intent it4 = new Intent(Intent.ACTION_SEND);
it4.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it4.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
it4.setType("audio/mp3");
startActivity(Intent.createChooser(it4, "Choose Email Client"));
搭配 createChooser 的好處是:系統會列出所有能處理該 type 的程式讓使用者選擇,避免你的 App 與某個特定郵件客戶端強綁在一起。
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
// 播放媒體庫中的指定項目
Uri uri2 = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it2 = new Intent(Intent.ACTION_VIEW, uri2);
startActivity(it2);
// 解除安裝
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
// 依套件名稱搜尋,pkg_name 是應用的完整套件路徑
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
// 開啟指定應用的詳細頁面
Uri uri2 = Uri.parse("market://details?id=app_id");
Intent it2 = new Intent(Intent.ACTION_VIEW, uri2);
startActivity(it2);
Intent 的用法可以歸納成兩條路:一是明確指定元件(setClass、setClassName、setComponent),適合跳轉自己 App 內的畫面;二是隱式宣告動作與資料型別(action + data + type),交給系統去媒合。後者正是 Android 鬆耦合設計的精髓——你的程式不需要知道撥號程式叫什麼名字,只要說清楚「我要打電話給這個號碼」就好。實務上搭配 createChooser,還能把選擇權交還給使用者,讓應用之間的互補關係更自然。
Android 開發實戰:網路連線、多執行緒與並發處理
Android 應用程式最常見的任務之一,就是從網路取得資料或把資料送到遠端伺服器。而這些操作的結果,通常正是你想呈現給使用者的新資料——也就是說,你需要更新用戶界面。
Android 上的 XML 與 JSON 實戰(上):從格式基礎到 Twitter feed 解析
簡介:這個兩 Part 的系列要談的是行動平台上處理資料交換格式的實務——XML 與 JavaScript Object Notation(JSON)。第一篇先把兩種格式
你的第一個 Android 應用程式:Hello, Android!
評估一個開發框架好不好上手,最快的方式就是試著用它寫出第一個「Hello, World!」。在 Android 上,這件事輕鬆得令人愉悅。以下示範完整流程。 要做的事
Android 混合應用程式實作(上):WebView 元件與使用者介面的搭建
簡介 很難想像還有哪一種技術比手機更普及。各家平台在銷售量與佔有率上激烈廝殺,而真正撐起這些平台的,是上面可用應用程式所帶來的體驗。iPhone 與 Android 就是