Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 初階開發

Intent 常用寫法整理大全

編輯:初階開發

Intent 是 Android 元件之間溝通的核心。以下依功能分類整理常見的 Intent 用法,方便查閱。

瀏覽網頁

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);

規劃路線

Uri uri = Uri.parse("https://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

撥號與打電話

只開啟撥號盤、由使用者決定是否按下通話:

Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);

直接撥出(需在 Manifest 中加上 <uses-permission android:name="android.permission.CALL_PHONE" />):

Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_CALL, uri);
startActivity(it);

傳送簡訊 / 多媒體訊息

呼叫簡訊撰寫程式:

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 it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);

夾帶圖片傳送 MMS:

Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra("sms_body", "some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType("image/png");
startActivity(it);

寄送電子郵件

Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);

透過選擇器讓使用者挑郵件客戶端,並帶入收件者與本文:

Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));

完整指定收件者、副本、主旨:

Intent it = new Intent(Intent.ACTION_SEND);
String[] tos = {"me@abc.com"};
String[] ccs = {"you@abc.com"};
it.putExtra(Intent.EXTRA_EMAIL, tos);
it.putExtra(Intent.EXTRA_CC, ccs);
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));

附加檔案:

Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
it.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));

播放媒體

Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

解除安裝與安裝

Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
Uri uninstallUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

Market 相關

搜尋應用(pkg_name 為應用的完整套件路徑):

Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

開啟應用詳情頁(app_id 為應用 ID,可在 Market 首頁點進應用後從網址列取得):

Uri uri = Uri.parse("market://details?id=app_id");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

網頁搜尋

Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "searchString");
startActivity(intent);

使用提醒

幾個實務上容易忽略的細節:

  • 凡是使用 ACTION_CALL 這類高風險動作,都必須在 Manifest 宣告對應權限,否則會在執行期被拒絕。
  • 以 content:// 或 file:// 指向的資源,在較新版本的 Android 上需要透過 FileProvider 授權,直接傳 URI 會引發例外。
  • createChooser 的好處是把「讓使用者選」這件事交給系統,避免應用被綁死在特定客戶端上。
  • 這些範例源自早期 Android 版本,其中的 market:// 配置與部分 action 在後續版本已更名或調整,實際使用時請對照當前官方文件。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved