標籤: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到底是什麼