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

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

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

	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别人的就算了。为毛要删去注释。北大啊北大~~

时间: 2024-08-29 23:33:28

application/vnd.android.package-archive到底是什么的相关文章

Android package属性、package name和Application ID三者的联系及区别

名词解释 package属性:在AndroidManifest.xml文件中. package name:应用程序的包名. Application ID:模块defaultConfig块下的applicationId属性. 设置Application ID 每个Android应用程序都有唯一一个类似Java包名的Application ID,比如com.example.myapp.在Android设备和Google应用商店上,Application ID是您应用的唯一标识.如果您想上传应用程序的

Android Application Fundamentals——Android应用程序基础知识

Application Fundamentals--应用程序基础知识 Key classes--关键类 Activity Service BroadcastReceiver ContentProvider Intent In this document--在这篇文章中 Application Components--应用程序组件 Activating components: intents--激活组件:意图 Shutting down components--关闭组件 The manifest

Content-Type: application/vnd.ms-excel&quot;&gt;

如果要将查询结果导出到Excel,只需将页面的Context-Type修改一下就可以了:   header( "Content-Type: application/vnd.ms-excel">如果希望能够提供那个打开/保存的对话框,Content-Disposition参数,Content-Disposition参数本来是为了在客户端另存文件时提供一个建议的文件名,但是考虑到安全的原因,就从规范中去掉了这个参数Content-Disposition参数:attachment --

Ubuntu 之 Personal Package Archive (PPA)

How do I use software from a PPA? To start installing and using software from a Personal Package Archive, you first need to tell Ubuntu where to find the PPA. Important: The contents of Personal Package Archives are not checked or monitored. You inst

继承Application实现Android数据共享

     Application类 在Android中,启动一个应用,首先会初始化Application,然后再通过它检查AndroidManifest.xml清单文件,选择须要首先启动的Activity. 在Activity中能够使用getApplication()方法获得该Application的实例,使用它就能够获得当前应用的主题.资源文件里的内容等,而且我们能够通过它来加入自己的全局属性,如User.比如开发一个游戏,须要保存分数,那么我们就能够继承Application. 首先,先写个

Android中Context到底是什么以及用法

这是Stackoverflow上一位网友的回答: 原地址:http://stackoverflow.com/questions/3572463/what-is-context-in-android As the name suggests, its the context of current state of the application/object. It lets newly created objects understand what has been going on. Typi

Errors running builder &quot;Android Package Builder&quot; on project &quot;***&quot;minvor version 52

博主差点被这个问题整死   - -! 原因: jdk版本过低,不匹配现在的sdk. 解决办法:下载最新的jdk1.8(附上地址http://www.androiddevtools.cn/),安装后去eclipse配置好并重启eclipse,问题解决.

Template design pattern application in android

The template method allow separate the generic method from a detail context(class) via inheritance Another advantage: 1.Inheritance implementation means that you will know the actual method implementation class before compilation,it is very big advan

Android软件自动更新(自定义处理,不使用第三方)

来源:http://www.android100.org/html/201406/08/21956.html XML放入网络空间---version.xml .url地址应该为正常***/*****.apk<update> <version>5</version> <name>yuanbao</name> <url>http://cdn.market.hiapk.com/data/upload//2012/06_27/13/yuanb