码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-05-18 13:02:26      阅读:1631      评论:0      收藏:0      [点我收藏+]

标签:android应用   application   app   apk   android   

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

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

	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]

看。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>

互联网媒体类型(Internet media type),原名叫“Type
 MIME”或“MIME”或在头信息中各种协议之后的内容种类(Content-type),他有两部分用来在Internet上鉴别数据格式。鉴别方法已经在RFC 2046中定义,使用在电子邮件通过SMTP。但是他已经被扩展到其他的协议当中,比如:HTTP或者SIP。
 一个Type MIME至少包括两个部分:一个类型和一个子类型和一个或多个其他需要的参数。比如,一个子类型text有一个可选参数charset用来表明字符编码;或者一个multipart类型的子类型定义了一个选项boundary。
 那些类型和子类型都以“x-”开头的是不标准的,他们不能被存储如果应用IANA。那些子类型以“vnd”开头的是商家信息。

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.


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

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一下。就能找到原来的版本。

也是醉了。copy别人的就算了。为毛要删去注释。北大啊北大~~


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

标签:android应用   application   app   apk   android   

原文地址:http://blog.csdn.net/vvaaiinn/article/details/45816507

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!