編輯: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);
傳送彩信:
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:someone@example.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "someone@example.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 = {"a@example.com"};
String[] ccs = {"b@example.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);
幾個實務上會碰到的問題:ACTION_CALL 屬於高風險權限,新版系統已大幅限制其可用性,一般應優先使用 ACTION_DIAL 把號碼帶進撥號盤由使用者確認。存取 /sdcard 的寫法在分區儲存(Scoped Storage)上路之後也需要改用 MediaStore 或 SAF。而 content://media/... 這類 URI 中的 ID 是隨裝置而變的,不能寫死,應透過查詢 MediaStore 動態取得。
Android 下拉刷新完全解析:一分鐘實作下拉刷新功能(附源碼)
最近專案中需要用到 ListView 的下拉刷新功能。起初想省事,直接從網上找現成的方案,但試了多個版本後發現效果都不理想——有些功能不完整或有 Bug,有些使用起來太過
Android JSON 解析入門
JSON 是一種輕量級的資料交換格式,常用於應用程式與伺服器之間傳輸資料。相比 XML,JSON 結構更簡潔,也更容易在 Android 中解析。本文說明 JSON 的基
Android XML 靜態資源與 R.java 使用指南
Android 開發中,XML 靜態資源與 Java 程式碼經常搭配使用。XML 負責描述版面與資源,Java 負責控制邏輯,而 R.java 則是兩者之間的橋梁。R.j
Android ListView 圓角外觀實作技巧
若希望 ListView 呈現圓角或對話框式外觀,可以透過背景資源快速實現。 實作方式 在 ListView 上設定 android:background 屬性,並指定