将自己的软件关联成Android系统默认打开程序


使用过Android设备的朋友当我们要打开某个文件。这里以文本为例,有的时候会弹出一个列表,选择使用其中一个软件来打开文本。作为开发人员如何将自己的软件加入到列表中呢。

我们通过设置AndroidManifest.xml文件即可代码如下:

  1.  <activity  
  2.             android:label="@string/app_name"  
  3.             android:launchMode="singleTask"  
  4.             android:name=".EsayNoteActivity"  
  5.             android:screenOrientation="portrait" >  
  6.             <intent-filter >  
  7.                 <action android:name="android.intent.action.MAIN" />  
  8.   
  9.                 <category android:name="android.intent.category.LAUNCHER" />  
  10.             </intent-filter>  
  11.             <intent-filter >  
  12.                 <action android:name="android.intent.action.VIEW" />  
  13.   
  14.                 <category android:name="android.intent.category.DEFAULT" />  
  15.   
  16.                 <data android:mimeType="text/plain" />  
  17.             </intent-filter>  
  18. </activity>  

这样当打开文本文件的时候我们自己的软件就在列表里了

主要是设置mimeType的类型,文本文件是:text/plain

在配置中程序设置关联之后,还有参数传递问题 需要在onCreate()里面添加如下代码:

  1. Intent intent = getIntent();   
  2. String action = intent.getAction();   
  3.    if (Intent.ACTION_VIEW.equals(action)) {   
  4.        TextView tv = (TextView) findViewById(R.id.tvText);   
  5.        tv.setText(intent.getDataString()); //显示文件路径   
  6.   }  

常用的类型还有:
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(同上,但主要用于表单提交时伴随文件上传的场合)

相关内容