將程式關聯成Android系統預設開啟程式

來源:互聯網
上載者:User

比如通過文檔查看器開啟一個文字檔時,會彈出一個可用來開啟的軟體列表;如何讓自己的軟體也出現在該列表中呢?

<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());}




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.