這段時間很忙,少來發帖了,今天再來爆一個....
Android上的靜默安裝似乎是個很誘人的功能,好多人都問這個問題。今天分享下實現靜默安裝的兩種方法,但當看完這篇文章後,仍會讓一些人失望滴。
Android把所有的Permission依據其潛在風險(屬性名稱為protectionLevel )劃分為四個等級,即"normal "、 "dangerous "、 "signature "、 "signatureOrSystem "。 INSTALL_PACKAGES屬於後兩者。讓我們看一下官方文檔對後兩類的描述吧。
"signature ": A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user's explicit approval.
"signatureOrSystem ": A permission that the system grants only to applications that are in the Android system image or that are signed with the same certificates as those in the system image. Please avoid using this option, as thesignature protection level should be sufficient for most needs and works regardless of exactly where applications are installed. The "signatureOrSystem " permission is used for certain special situations where multiple vendors have applications built into a system image and need to share specific features explicitly because they are being built together.
所以,這兒介紹的兩種方法各自需要的苛刻條件如下:
1.內建到ROM。即APK包的安裝位置是/system/app下。
2.使用APK的目標安裝系統同樣的簽名。
好了,先不管這些苛刻的條件,下面講下如何編寫直接安裝APK的代碼,這兒使用pm install <apk_path>命令,而不是繁雜的未公開的PackageManager.install()方法。
- //This code come form Sodino.Email:sodinoopen@hotmail.com
- String[] args = { "pm", "install", "-r", apkAbsolutePath };
- String result = "";
- ProcessBuilder processBuilder = new ProcessBuilder(args);
- Process process = null;
- InputStream errIs = null;
- InputStream inIs = null;
- try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int read = -1;
- process = processBuilder.start();
- errIs = process.getErrorStream();
- while ((read = errIs.read()) != -1) {
- baos.write(read);
- }
- baos.write('\n');
- inIs = process.getInputStream();
- while ((read = inIs.read()) != -1) {
- baos.write(read);
- }
- byte[] data = baos.toByteArray();
- result = new String(data);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (errIs != null) {
- errIs.close();
- }
- if (inIs != null) {
- inIs.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (process != null) {
- process.destroy();
- }
- }
- return result;
複製代碼
代碼執行後,如果安裝成功的話擷取到的result值是“ pkg: /data/local/tmp/Calculator.apk \nSuccess”,如果是失敗的話,則沒有結尾的“Success”。
安裝代碼有了,現在開始介紹第一種方法,將你自己的APK內建到ROM中。前提是,你這手機已經刷機過並且保留了recovery-windows.bat/recover-linux.sh 檔案。
針對HTC-Legend的具體操作步驟為:
1.USB串連你的裝置然後在命令列輸入 "adb reboot recovery" ,機子重啟,啟動後將顯示一個紅色的三角形和箭頭表徵圖
2 .(在PC下)進入到你的刷機檔案夾然後運行 './recover-linux.sh' ,螢幕將顯示綠色的菜單
3 .如果得到的結果是 "error:device not found" ,運行 "./adb-linux kill-server" 後再一次運行 './recovery-linux.sh' 直到顯示綠色菜單.
4 .執行 "adb shell mount /dev/block/mtdblock3 /system" ,至此,可對/system進行寫操作。
5.在PC上運行命令:adb push <your_apk_path> /system/<your_apk_name>。至此,內建成功。
第二種方法,需要先打一個未簽名的APK包,然後用系統簽名對其進行簽名。這個方面的東西在我之前的一篇博文已說明,這兒就不重複了。[Android]使用platform密鑰來給apk檔案簽名的命令
由於HTC-Legend是“原裝”的,所以靜默安裝倒是順利。但對於一些MOTO或樂Phone的手機,一般上是不支援的。