Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 常用系統 Intent.Action 範例集

編輯:關於 Android 程式設計

Android 內建了大量共用的 Intent Action,用對了就能省下自己實作的工夫,直接呼叫系統或其他 App 既有的功能。以下整理常用的幾個,並附上可執行的範例。

常用 Action 對照

常數字串值用途
ACTION_MAINandroid.intent.action.MAIN應用程式入口
ACTION_VIEWandroid.intent.action.VIEW顯示資料給使用者
ACTION_ATTACH_DATAandroid.intent.action.ATTACH_DATA指明附加資訊給其他地方的資料
ACTION_EDITandroid.intent.action.EDIT顯示可編輯的資料
ACTION_PICKandroid.intent.action.PICK選擇資料
ACTION_CHOOSERandroid.intent.action.CHOOSER顯示一個 Activity 選擇器
ACTION_GET_CONTENTandroid.intent.action.GET_CONTENT取得內容
ACTION_DIALandroid.intent.action.DIAL顯示撥號面板
ACTION_CALLandroid.intent.action.CALL直接撥打電話
ACTION_SENDandroid.intent.action.SEND發送資料
ACTION_SENDTOandroid.intent.action.SENDTO選擇發送管道
ACTION_ANSWERandroid.intent.action.ANSWER應答電話
ACTION_INSERTandroid.intent.action.INSERT插入資料
ACTION_DELETEandroid.intent.action.DELETE刪除資料
ACTION_RUNandroid.intent.action.RUN執行資料
ACTION_SYNCandroid.intent.action.SYNC同步資料
ACTION_PICK_ACTIVITYandroid.intent.action.PICK_ACTIVITY選擇 Activity
ACTION_SEARCHandroid.intent.action.SEARCH搜尋
ACTION_WEB_SEARCHandroid.intent.action.WEB_SEARCH網頁搜尋
ACTION_FACTORY_TESTandroid.intent.action.FACTORY_TEST工廠測試入口點

原始資料中 ACTION_DIAL 與 ACTION_CALL 兩列的說明文字互相對調了,這裡已修正——DIAL 只是開啟撥號面板讓使用者確認,CALL 才是直接撥出。

版面

<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="5dp"
            android:text="Android 中常用系統 Intent" />

        <Button android:id="@+id/intent_call_btn"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:layout_marginTop="2dp" android:text="撥打電話" />

        <Button android:id="@+id/intent_sms_btn"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:layout_marginTop="2dp" android:text="發送短信" />

        <Button android:id="@+id/intent_email_btn"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:layout_marginTop="2dp" android:text="發送郵件" />

        <Button android:id="@+id/intent_net_btn"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:layout_marginTop="2dp" android:text="打開網頁" />

        <Button android:id="@+id/intent_pic_btn"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:layout_marginTop="2dp" android:text="發送圖片" />

        <Button android:id="@+id/intent_media_btn"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:layout_marginTop="2dp" android:text="打開媒體" />

        <Button android:id="@+id/intent_search_btn"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:layout_marginTop="2dp" android:text="搜尋" />

        <Button android:id="@+id/intent_install_btn"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:layout_marginTop="2dp" android:text="安裝軟體" />

        <Button android:id="@+id/intent_unstall_btn"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:layout_marginTop="2dp" android:text="卸載軟體" />
    </LinearLayout>
</ScrollView>

Activity 程式碼

public class MainActivity extends Activity implements OnClickListener {

    private Button callBtn, smsBtn, emailBtn, browseBtn, searchBtn;
    private Button installBtn, unInstallBtn, mediaBtn, picBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViewsById();
        initListeners();
    }

    private void initViewsById() {
        callBtn      = (Button) findViewById(R.id.intent_call_btn);
        smsBtn       = (Button) findViewById(R.id.intent_sms_btn);
        emailBtn     = (Button) findViewById(R.id.intent_email_btn);
        browseBtn    = (Button) findViewById(R.id.intent_net_btn);
        picBtn       = (Button) findViewById(R.id.intent_pic_btn);
        installBtn   = (Button) findViewById(R.id.intent_install_btn);
        unInstallBtn = (Button) findViewById(R.id.intent_unstall_btn);
        mediaBtn     = (Button) findViewById(R.id.intent_media_btn);
        searchBtn    = (Button) findViewById(R.id.intent_search_btn);
    }

    private void initListeners() {
        callBtn.setOnClickListener(this);
        smsBtn.setOnClickListener(this);
        emailBtn.setOnClickListener(this);
        browseBtn.setOnClickListener(this);
        picBtn.setOnClickListener(this);
        installBtn.setOnClickListener(this);
        unInstallBtn.setOnClickListener(this);
        mediaBtn.setOnClickListener(this);
        searchBtn.setOnClickListener(this);
    }

    /** 瀏覽網頁 */
    private void netBrowse() {
        Uri uri = Uri.parse("https://www.baidu.com");
        startActivity(new Intent(Intent.ACTION_VIEW, uri));
    }

    /** 播放媒體 */
    private void playMedia() {
        Uri uri = Uri.withAppendedPath(
                MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
        startActivity(new Intent(Intent.ACTION_VIEW, uri));
    }

    /** 網頁搜尋 */
    private void search() {
        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
        intent.putExtra(SearchManager.QUERY, "android");
        startActivity(intent);
    }

    /** 撥打電話(僅開啟撥號面板) */
    private void callTelphone() {
        Uri uri = Uri.parse("tel:10086");
        startActivity(new Intent(Intent.ACTION_DIAL, uri));
    }

    /** 發送簡訊 */
    private void sendSms() {
        Uri uri = Uri.parse("smsto:10086");
        startActivity(new Intent(Intent.ACTION_SENDTO, uri));
    }

    /** 發送圖片(彩信) */
    @SuppressLint("SdCardPath")
    private void sendPicSms() {
        Uri imguri = Uri.parse("/mnt/sdcard/abc.png");
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, imguri);
        intent.setType("image/png");
        startActivity(Intent.createChooser(intent, "Send Image To:"));
    }

    /** 發送郵件 */
    private void sendEmail() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"shoujianren@163.com"});
        intent.putExtra(Intent.EXTRA_CC, new String[]{"chaosong@163.com"});
        intent.putExtra(Intent.EXTRA_SUBJECT, "朋友,您好!");
        intent.putExtra(Intent.EXTRA_TEXT, "好多的內容呀........");
        intent.setType("message/rfc822");
        startActivity(Intent.createChooser(intent, "請選擇客戶端郵箱!"));
    }

    /** 安裝應用 */
    private void installSotf() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/tutu.apk")),
                "application/vnd.android.package-archive");
        startActivity(intent);
    }

    /** 卸載應用 */
    private void uninstallSoft() {
        Uri uri = Uri.fromParts("package", "tutu.ch05", null);
        startActivity(new Intent(Intent.ACTION_DELETE, uri));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.intent_call_btn:      callTelphone();  break;
            case R.id.intent_sms_btn:       sendSms();       break;
            case R.id.intent_email_btn:     sendEmail();     break;
            case R.id.intent_pic_btn:       sendPicSms();    break;
            case R.id.intent_net_btn:       netBrowse();     break;
            case R.id.intent_search_btn:    search();        break;
            case R.id.intent_install_btn:   installSotf();   break;
            case R.id.intent_unstall_btn:   uninstallSoft(); break;
            case R.id.intent_media_btn:     playMedia();     break;
        }
    }
}

需要的權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

現代 Android 上的必要修正

這份範例寫於幾年前,直接跑在當前版本上會遇到幾個問題,值得逐項說明。

直接撥號需要權限。ACTION_CALL 需要 CALL_PHONE 權限(且為執行期動態申請)。這也是為什麼範例刻意選用 ACTION_DIAL——它只開啟撥號面板、由使用者按下通話鈕,不需要任何權限,是更友善也更安全的做法。

file:// URI 已不能用於跨應用。sendPicSms() 與 installSotf() 都傳了 file:// URI,在 Android 7.0 以上會拋出 FileUriExposedException。必須改用 FileProvider.getUriForFile() 取得 content URI,並加上 FLAG_GRANT_READ_URI_PERMISSION。

安裝未知來源應用受限。從 Android 8.0 起,REQUEST_INSTALL_PACKAGES 權限加上「逐應用授權」機制取代了舊的全域「未知來源」開關。若 App 要能安裝 APK,必須引導使用者到系統設定中為自己的 App 開啟這項權限。

MOUNT_UNMOUNT_FILESYSTEMS 不該使用。這是一個 signature 等級的權限,普通應用拿不到,也不應申請。

SearchManager.QUERY 的使用。ACTION_WEB_SEARCH 已被標記為 deprecated,建議改用 ACTION_WEB_SEARCH 的替代方案或直接以 ACTION_VIEW 搭配搜尋 URL。

這些修正做完之後,這份範例集就能作為日常開發的參考手冊使用了。

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