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

Android 學習筆記:用 Data 與 Type 讓系統自動挑選合適的 App 開啟檔案

編輯:關於 Android 程式設計

仔細想想,手機上開啟一個檔案其實只有兩條路:

一是從檔案出發——檔案管理器知道副檔名,於是去找能處理這個類型的 App;二是從 App 出發——你在 Gallery 裡點選某張照片,Gallery 於是去系統裡問:「誰能顯示 image/jpeg?」

兩條路的方向相反,但底層依賴的是同一套機制:Intent 加上 data 的 URI 與 MIME type。本文用一個小 demo 把這件事跑通:點擊按鈕播放歌曲、點擊另一個按鈕用瀏覽器打開網頁。

先把測試檔案塞進模擬器

要播放本機音樂,得先有音樂檔。步驟如下:

1. 模擬器啟動後,在右上角把視角切換到 DDMS,左側 Devices 面板會列出模擬器的線程資訊。

2. 在右側切到 File Explorer,定位到 /mnt/media_rw/sdcard/。

3. 點擊工具列上的「Push a file onto the device」圖示(由上往下數第二個),選好檔案推送進去。檔名建議用純小寫英文,最保險。

4. 推送成功後,該檔案就會出現在 /mnt/media_rw/sdcard/ 底下。

效果預覽

點擊「播放歌曲」按鈕,系統會自動挑出本機播放器並播放指定曲目:

點擊「打開瀏覽器」按鈕,則會跳到預設瀏覽器並載入指定網址:

版面配置

activity_main.xml 只需要兩個按鈕:

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

    <Button
        android:id="@+id/bt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="點我播放歌曲"
        android:textSize="20sp" />

    <Button
        android:id="@+id/bt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/bt"
        android:layout_marginTop="20dp"
        android:text="點我打開瀏覽器網站"
        android:textSize="20sp" />

</RelativeLayout>

MainActivity 的關鍵三行

public class MainActivity extends Activity implements OnClickListener {

    private Button bt, bt1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt  = (Button) findViewById(R.id.bt);
        bt1 = (Button) findViewById(R.id.bt1);
        bt.setOnClickListener(this);
        bt1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt:
                // 用「資料 + 型別」精確描述:這是什麼檔案、該怎麼處理
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(
                        Uri.parse("file:///mnt/sdcard/inLove.mp3"),
                        "audio/mp3");
                startActivity(intent);
                break;

            case R.id.bt1:
                // 只給 URI 就夠了,http 協議本身已足以讓系統判斷
                Intent intent1 = new Intent();
                intent1.setAction(Intent.ACTION_VIEW);
                intent1.setData(Uri.parse("https://www.baidu.com"));
                startActivity(intent1);
                break;

            default:
                break;
        }
    }
}

跟以往.Intent 用法差在哪

以前我們多半只寫 new Intent(this, TargetActivity.class),屬於顯式 Intent——明確指定要啟動誰。這次改用隱式 Intent,只描述「我想做什麼、對象是什麼」,把挑選 App 的權利交還給系統。

具體來說多了兩個設定:

1. setAction(Intent.ACTION_VIEW):宣告意圖是「檢視」。

2. setDataAndType(uri, mimeType):把資源位置與內容型別一起打包成 URI(Uniform Resource Identifier)。若是瀏覽器跳轉,因為 http 協議已足以推斷型別,只呼叫 setData() 即可。

而 Intent-Filter 中的 data 標籤有一個 mimeType 屬性,作用是告訴系統「本 Activity 能處理哪 kinds 的檔案」。例如寫成 "text/plain" 就代表能處理 .txt;圖片則依格式分為 image/jpeg、image/gif、image/png 等等,音訊 likewise 有 audio/mp3、audio/mpeg 等。

URI 與 URL 的關係

可以粗略地說,URL 是 URI 中「有具體協議、方便人記憶」的那一類。像 www.baidu.com 這種寫法並不是真實位址,背後仍要解析成一串類似 192.168.1.0 的 IP(順帶一提,這其實是常見的路由器位址)。

歌曲有歌曲的型別(mp3),網址有網址的型別(http),每一種型別都對應一個 MIME type(例如 audio/mp3)。系統正是靠這組資訊完成配對。完整的 MIME type 對照表可以參考官方與社群維護的 MIME 參考手冊。

小結

想讓 App「能被別人叫去開檔案」,就在 AndroidManifest 的 Intent-Filter 裡宣告 data 的 scheme 與 mimeType;想讓 App「叫別人來開檔案」,就用 ACTION_VIEW 加上 setData/setDataAndType。把這兩件事想通,Android 的檔案與應用程式之間那張配對網絡就不再神祕了。

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