分享

Android精选

 w_hf的图书馆 2012-06-05

精选

3227

[Android]实现静默安装APK的两种方法

2011-3-11 00:51|发布者: Vincent|评论: 9

        这段时间很忙,少来发帖了,今天再来爆一个....

        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()方法。
  1. //This code come form Sodino.Email:sodinoopen@hotmail.com
  2. String[] args = { "pm", "install", "-r", apkAbsolutePath };  
  3. String result = "";  
  4. ProcessBuilder processBuilder = new ProcessBuilder(args);  
  5. Process process = null;  
  6. InputStream errIs = null;  
  7. InputStream inIs = null;  
  8. try {  
  9.     ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  10.     int read = -1;  
  11.     process = processBuilder.start();  
  12.     errIs = process.getErrorStream();  
  13.     while ((read = errIs.read()) != -1) {  
  14.         baos.write(read);  
  15.     }  
  16.     baos.write('\n');  
  17.     inIs = process.getInputStream();  
  18.     while ((read = inIs.read()) != -1) {  
  19.         baos.write(read);  
  20.     }  
  21.     byte[] data = baos.toByteArray();  
  22.     result = new String(data);  
  23. } catch (IOException e) {  
  24.     e.printStackTrace();  
  25. } catch (Exception e) {  
  26.     e.printStackTrace();  
  27. } finally {  
  28.     try {  
  29.         if (errIs != null) {  
  30.             errIs.close();  
  31.         }  
  32.         if (inIs != null) {  
  33.             inIs.close();  
  34.         }  
  35.     } catch (IOException e) {  
  36.         e.printStackTrace();  
  37.     }  
  38.     if (process != null) {  
  39.         process.destroy();  
  40.     }  
  41. }  
  42. 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的手机,一般上是不支持的。

     OK,讲完了,睡觉。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多