比如通過文檔查看器開啟一個文字檔時,會彈出一個可用來開啟的軟體列表;如何讓自己的軟體也出現在該列表中呢?
<activity android:name=".EasyNote" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> <data android:mimeType="text/plain"></data> </intent-filter> </activity>
第一個<intent-filter>標籤是每個程式都有的,關鍵是要添加第二個!這樣你的應用程式就會出現在預設開啟列表了。。。
注意需要將mimeType修改成你需要的類型,文字檔當然就是:text/plain
還有其它常用的如:
- text/plain(純文字)
- text/html(HTML文檔)
- application/xhtml+xml(XHTML文檔)
- image/gif(GIF映像)
- image/jpeg(JPEG映像)【PHP中為:image/pjpeg】
- image/png(PNG映像)【PHP中為:image/x-png】
- video/mpeg(MPEG動畫)
- application/octet-stream(任意的位元據)
- application/pdf(PDF文檔)
- application/msword(Microsoft Word檔案)
- message/rfc822(RFC 822形式)
- multipart/alternative(HTML郵件的HTML形式和純文字形式,相同內容使用不同形式表示)
- application/x-www-form-urlencoded(使用HTTP的POST方法提交的表單)
- multipart/form-data(同上,但主要用於表單提交時伴隨檔案上傳的場合)
關於mimeType更多資訊可以瀏覽:http://blog.csdn.net/tt5267621/article/details/7173972
將程式設定關聯之後,還需要處理參數傳遞問題! 需要在onCreate()裡面添加如下樣本判斷代碼(未測試):
Intent intent = getIntent();String action = intent.getAction();if(intent.ACTION_VIEW.equals(action)){ TextView tv = (TextView)findViewById(R.id.tvText); tv.setText(intent.getDataString());}