編輯:關於 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>
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;
}
}
}
以前我們多半只寫 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 等。
可以粗略地說,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 的檔案與應用程式之間那張配對網絡就不再神祕了。
u-boot 串口與 stdio、console 初始化及相關操作詳解(三)
console 是構建在 stdio 之上的,console 的初始化是 board_r 中最後收尾的操作。 console 的初始化函數 console_init_r
Android 的 MVC、MVP 與 MVVM 架構解析(上):把 MVC 講透
前言:工欲善其事,必先利其器 工作一段時間之後,對這十個字的體會最深。剛入行時,並不真的理解自己在做什麼,上面分派任務,我就埋頭去做,很少停下來思考。後來才慢慢發現,做得
NoHttp 源碼解析(一):請求模組全拆解
市面上的 Http 框架很多,上層的有 NoHttp、Retrofit、Volley、android-async-http,底層的有 HttpURLConnection、
Android 原始碼中的結構型設計模式解析
結構型模式關注的是「類與物件如何組裝」。其中的適配器、外觀、裝飾、代理都屬於包裝模式——都是對另外的類或物件做包裝,只是意圖各不相同: 適配器模式:透過包裝,把既有介面