将自己的软件关联成Android系统默认打开程序
将自己的软件关联成Android系统默认打开程序
使用过Android设备的朋友当我们要打开某个文件。这里以文本为例,有的时候会弹出一个列表,选择使用其中一个软件来打开文本。作为开发人员如何将自己的软件加入到列表中呢。
我们通过设置AndroidManifest.xml文件即可代码如下:
- <activity
- android:label="@string/app_name"
- android:launchMode="singleTask"
- android:name=".EsayNoteActivity"
- 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" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="text/plain" />
- </intent-filter>
- </activity>
这样当打开文本文件的时候我们自己的软件就在列表里了
主要是设置mimeType的类型,文本文件是:text/plain
在配置中程序设置关联之后,还有参数传递问题 需要在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()); //显示文件路径
- }
常用的类型还有:
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(同上,但主要用于表单提交时伴随文件上传的场合)
评论暂时关闭