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

Android Intent 常用寫法整理:撥號、簡訊、地圖與市集

編輯:初階開發

Intent 是 Android 的通用語言。以下把實務上最常翻閱的幾組寫法整理成清單,需要的時候直接查。

開啟網頁與地圖

// 開啟瀏覽器
Uri uri = Uri.parse("https://google.com");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
// 顯示地圖座標
Uri uri = Uri.parse("geo:38.899533,-77.036476");
startActivity(new Intent(Intent.ACTION_VIEW, uri));

// 其他 geo URI 形式:
// geo:latitude,longitude
// geo:latitude,longitude?z=zoom
// geo:0,0?q=my+street+address
// geo:0,0?q=business+near+city
// google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom
// 路徑規劃
Uri uri = Uri.parse("https://maps.google.com/maps?f=d"
    + "&saddr=startLat%20startLng"
    + "&daddr=endLat%20endLng"
    + "&hl=en");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
// 座標需帶 6 位小數,例如 50.123456

電話

// 叫出撥號程式(不立即撥出)
Uri uri = Uri.parse("tel:0800000123");
startActivity(new Intent(Intent.ACTION_DIAL, uri));

// 直接撥出
Uri uri2 = Uri.parse("tel:0800000123");
Intent it = new Intent(Intent.ACTION_CALL, uri2);
startActivity(it);
// 需先在 Manifest 加上:
// <uses-permission android:name="android.permission.CALL_PHONE" />

ACTION_DIAL 只會把號碼填進撥號介面,由使用者決定要不要按下撥號;ACTION_CALL 則是直接撥出,屬於危險權限,需要執行期授權。除非有明確需求,否則優先用前者。

簡訊與多媒體訊息

// 叫出行訊應用並帶入內容
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");
startActivity(new Intent(Intent.ACTION_SENDTO, uri));
// 帶主旨與內文,並讓使用者挑用戶端
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 it2 = new Intent(Intent.ACTION_SEND);
String[] tos = {"me@abc.com"};
String[] ccs = {"you@abc.com"};
it2.putExtra(Intent.EXTRA_EMAIL, tos);
it2.putExtra(Intent.EXTRA_CC, ccs);
it2.putExtra(Intent.EXTRA_TEXT, "The email body text");
it2.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it2.setType("message/rfc822");
startActivity(Intent.createChooser(it2, "Choose Email Client"));
// 夾帶附件
Intent it3 = new Intent(Intent.ACTION_SEND);
it3.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it3.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/mysong.mp3"));
it3.setType("audio/mp3");
startActivity(Intent.createChooser(it3, "Choose Email Client"));

播放多媒體

// 播放記憶卡上的 mp3
Uri uri = Uri.parse("file:///sdcard/song.mp3");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.setType("audio/mp3");
startActivity(it);

// 播放媒體庫中的特定項目
Uri uri2 = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
startActivity(new Intent(Intent.ACTION_VIEW, uri2));

應用市集相關

// 搜尋某個應用
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
startActivity(new Intent(Intent.ACTION_VIEW, uri));

// 顯示某應用的詳細資訊(app_id 可在市集網頁版網址列找到)
Uri uri2 = Uri.parse("market://details?id=app_id");
startActivity(new Intent(Intent.ACTION_VIEW, uri2));

移除應用

Uri uri = Uri.fromParts("package", strPackageName, null);
startActivity(new Intent(Intent.ACTION_DELETE, uri));

實務提醒

小心 ActivityNotFoundException。上述意圖都依賴裝置上有對應的程式。平板、電視盒或精簡 ROM 常常沒有撥號、簡訊、市集應用,直接 startActivity 就會崩潰。正式環境請一律包 try-catch,或用 resolveActivity() 先確認有沒有人接。

file:// URI 的限制。從 Android 7.0 起,對外分享檔案必須改用 FileProvider 產生 content:// URI 並以 FLAG_GRANT_READ_URI_PERMISSION 授權,直接傳 file:// 會被系統攔下。

setType 不能省。ACTION_SEND 系列若沒有指定 MIME 型別,很多收件程式會直接把自己從候選清單中排除,看起來就像「功能不見了」。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved