application/vnd.android.package-archive到底是什么

3 篇文章 0 订阅
1 篇文章 0 订阅

在拜读组里北大研二的安卓代码的时候,

读到登录前检测版本后更新的代码。发现了一个不懂的地方。

	void update() {
		Intent intent = new Intent(Intent.ACTION_VIEW);

		intent.setDataAndType(Uri.fromFile(new File(Environment
				.getExternalStorageDirectory(), m_appNameStr)),
				"application/vnd.android.package-archive");
		Log.e("msg", Environment.getExternalStorageDirectory() + ""
				+ m_appNameStr);
		startActivity(intent);
	}
其中m_appNameStr 是下载到本地的APP的名字。

在intent.setDataAndType中,前面是读取sd存储卡的apk的位置,

application/vnd.android.package-archive
代表什么呢。

直接搜索这个,发现大家的代码中都有这个部分出现,但是没有解释这个到底是什么。

首先WIKI:http://zh.wikipedia.org/wiki/APK

Android 应用程序包文件 (APK) 是一种Android操作系统上的应用程序安装文件格式,其英文全称为 “application package file” 。一个Android应用程序的代码想要在Android设备上运行,必须先进行编译,然后被打包成为一个被Android系统所能识别的文件才可以被运行,而这种能被Android系统识别并运行的文件格式便是“APK”。 一个APK文件内包含被编译的代码文件(.dex 文件),文件资源(resources), assets,证书(certificates),和清单文件(manifest file)。[1][2][3][4]

APK 文件基于 ZIP 文件格式,它与JAR文件的构造方式相似。它的互联网媒体类型application/vnd.android.package-archive.[5]

<pre name="code" class="html"></pre><span style="font-size:18px"><span style="color:rgb(37,37,37); font-family:sans-serif; font-size:14px; line-height:22.3999996185303px"><span style="white-space:pre"></span></span></span><pre name="code" class="java"><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">application/vnd.android.package-archive 为APK的互联网媒体类型。 那互联网媒体类型 是什么东西呢。以前没有听说过。 再一次看看WIKI:<a target=_blank href="http://zh.wikipedia.org/wiki/%E4%BA%92%E8%81%94%E7%BD%91%E5%AA%92%E4%BD%93%E7%B1%BB%E5%9E%8B">http://zh.wikipedia.org/wiki/%E4%BA%92%E8%81%94%E7%BD%91%E5%AA%92%E4%BD%93%E7%B1%BB%E5%9E%8B</a>
</span>
<span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">互联网媒体类型</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">(Internet media type),原名叫“</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">Type MIME</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">”或“</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/MIME" title="MIME" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">MIME</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">”或在头信息中各种协议之后的内容种类(</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">Content-type</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">),他有两部分用来在</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/Internet" title="Internet" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">Internet</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">上鉴别</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F" title="数据格式" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">数据格式</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">。鉴别方法已经在</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/RFC" title="RFC" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">RFC</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);"> 2046中定义,使用在电子邮件通过</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/SMTP" title="SMTP" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">SMTP</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">。但是他已经被扩展到其他的协议当中,比如:</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/HTTP" title="HTTP" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">HTTP</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">或者</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/SIP" title="SIP" class="mw-disambig" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">SIP</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">。 一个Type MIME至少包括两个部分:一个类型和一个子类型和一个或多个其他需要的参数。比如,一个子类型</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">text</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">有一个可选参数charset用来表明</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81" title="字符编码" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">字符编码</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">;或者一个</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">multipart</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">类型的子类型定义了一个选项</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">boundary</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">。 那些类型和子类型都以“x-”开头的是不标准的,他们不能被存储如果应用</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/IANA" title="IANA" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">IANA</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">。那些子类型以“vnd”开头的是商家信息。</span>

</pre><span style="font-size:18px"><span style="color:rgb(37,37,37); font-family:sans-serif; font-size:14px; line-height:22.3999996185303px"><span style="white-space:pre"></span></span></span><pre name="code" class="java"><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">application/vnd.android.package-archive 为APK的互联网媒体类型。 那互联网媒体类型 是什么东西呢。以前没有听说过。 再一次看看WIKI:<a target=_blank href="http://zh.wikipedia.org/wiki/%E4%BA%92%E8%81%94%E7%BD%91%E5%AA%92%E4%BD%93%E7%B1%BB%E5%9E%8B">http://zh.wikipedia.org/wiki/%E4%BA%92%E8%81%94%E7%BD%91%E5%AA%92%E4%BD%93%E7%B1%BB%E5%9E%8B</a></span>
 
<span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">互联网媒体类型</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">(Internet media type),原名叫“</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">Type MIME</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">”或“</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/MIME" title="MIME" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">MIME</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">”或在头信息中各种协议之后的内容种类(</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">Content-type</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">),他有两部分用来在</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/Internet" title="Internet" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">Internet</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">上鉴别</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F" title="数据格式" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">数据格式</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">。鉴别方法已经在</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/RFC" title="RFC" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">RFC</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);"> 2046中定义,使用在电子邮件通过</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/SMTP" title="SMTP" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">SMTP</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">。但是他已经被扩展到其他的协议当中,比如:</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/HTTP" title="HTTP" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">HTTP</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">或者</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/SIP" title="SIP" class="mw-disambig" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">SIP</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">。 一个Type MIME至少包括两个部分:一个类型和一个子类型和一个或多个其他需要的参数。比如,一个子类型</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">text</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">有一个可选参数charset用来表明</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81" title="字符编码" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">字符编码</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">;或者一个</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">multipart</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">类型的子类型定义了一个选项</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">boundary</span><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">。 那些类型和子类型都以“x-”开头的是不标准的,他们不能被存储如果应用</span><a target=_blank target="_blank" href="http://zh.wikipedia.org/wiki/IANA" title="IANA" class="mw-redirect" style="text-decoration: none; color: rgb(11, 0, 128); font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">IANA</a><span style="font-family: sans-serif; line-height: 22.3999996185303px; background-color: rgb(255, 255, 255);">。那些子类型以“vnd”开头的是商家信息。</span>

vnd  = vendor   stackoverflow大神解释如下。

Types or subtypes that begin with x- are nonstandard (they are not registered with IANA). Subtypes that begin with vnd are vendor-specific. Subtypes in the personal or vanity tree begin with prs.


最后在看下intent.setDataAndType(Uri.fromFile(new File(Environment
		.getExternalStorageDirectory(), m_appNameStr)),
<span style="white-space:pre">		</span>"application/vnd.android.package-archive");
这段代码

Intent android.content. Intent.setDataAndType( Uri data,  String type)
参数:
data The Uri of the data this intent is now targeting.
type The MIME type of the data being handled by this intent.
返回:
Returns the same Intent object, for chaining multiple calls into a single statement.

参数要求一个Uri 和一个互联网媒体类型,返回一个Intent对象。

通过setDataAndType  就可以实现更新,下载,打开新应用等功能。


安装APK

String dirPath = "/data/data/" + getPackageName() + "/files/test.apk"; //文件需有可读权限  
Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);  
intent.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");  
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
startActivity(intent); 


最后吐槽一下,看代码真的是一件恶心的事情。特别是没有注释的代码。

不过在看的途中发现,几乎所有代码都是在网上整体拔下来的,看不懂的地方直接google一下。就能找到原来的版本。

也是醉了。


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值