Android上的静默安装似乎是个很诱人的功能,好多人都问这个问题。今天分享下实现静默安装的两种方法,但当看完这篇文章后,仍会让一些人失望滴。

Android把所有的Permission依据其潜在风险(属性名为protectionLevel)划分为四个等级,即"normal"、"dangerous"、"signature"、"signatureOrSystem"。INSTALL_PACKAGES属于后两者。让我们看一下官方文档对后两类的描述吧。

"signature":A permission that the system grantsonly 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 toapplications that are in the Android system imageorthat aresigned with the same certificates as those in the system image.Please avoid using this option, as thesignatureprotection 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()方法。

view plain
  1. String[]args={"pm","install","-r",apkAbsolutePath};
  2. Stringresult="";
  3. ProcessBuilderprocessBuilder=newProcessBuilder(args);
  4. Processprocess=null;
  5. InputStreamerrIs=null;
  6. InputStreaminIs=null;
  7. try{
  8. ByteArrayOutputStreambaos=newByteArrayOutputStream();
  9. intread=-1;
  10. process=processBuilder.start();
  11. errIs=process.getErrorStream();
  12. while((read=errIs.read())!=-1){
  13. baos.write(read);
  14. }
  15. baos.write('/n');
  16. inIs=process.getInputStream();
  17. while((read=inIs.read())!=-1){
  18. baos.write(read);
  19. }
  20. byte[]data=baos.toByteArray();
  21. result=newString(data);
  22. }catch(IOExceptione){
  23. e.printStackTrace();
  24. }catch(Exceptione){
  25. e.printStackTrace();
  26. }finally{
  27. try{
  28. if(errIs!=null){
  29. errIs.close();
  30. }
  31. if(inIs!=null){
  32. inIs.close();
  33. }
  34. }catch(IOExceptione){
  35. e.printStackTrace();
  36. }
  37. if(process!=null){
  38. process.destroy();
  39. }
  40. }
  41. returnresult;

代码执行后,如果安装成功的话获取到的result值是“ pkg: /data/local/tmp/Calculator.apk /nSuccess”,如果是失败的话,则没有结尾的“Success”。

安装代码有了,现在开始介绍第一种方法,将你自己的APK内置到ROM中。前提是,你这手机已经刷机过并且保留了recovery-windows.bat/recover-linux.sh文件。

针对HTC-Legend的具体操作步骤为:

1.USB连接你的设备然后在命令行输入"adbrebootrecovery",机子重启,启动后将显示一个红色的三角形和箭头图标

2.(在PC下)进入到你的刷机文件夹然后运行'./recover-linux.sh',屏幕将显示绿色的菜单

3.如果得到的结果是"error:devicenotfound",运行"./adb-linuxkill-server"后再一次运行'./recovery-linux.sh'直到显示绿色菜单.

4.执行"adbshellmount/dev/block/mtdblock3/system",至此,可对/system进行写操作。

5.在PC上运行命令:adb push <your_apk_path> /system/<your_apk_name>。至此,内置成功。

第二种方法,需要先打一个未签名的APK包,然后用系统签名对其进行签名。这个方面的东西在我之前的一篇博文已说明,这儿就不重复了。[Android]使用platform密钥来给apk文件签名的命令

由于HTC-Legend是“原装”的,所以静默安装倒是顺利。但对于一些MOTO或乐Phone的手机,一般上是不支持的。


以上这两种方法都在AndroidManifest中声明android.permission.INSTALL_PACKAGES,有一点比较奇怪的是执行“int result = checkCallingOrSelfPermission(Intent.ACTION_PACKAGE_INSTALL)”,result的值为android.content.pm.PackageManager.PERMISSION_DENIED而不是PERMISSION_GRANTED。

更多相关文章

  1. Pycharm安装PyQt5的详细教程
  2. Android(安卓)开发环境配置--实现第一个app “hello world!”
  3. Android应用开发提高系列(5)——Android动态加载(下)——加载已安装A
  4. Android(安卓)SDK 2.3与Eclipse最新版开发环境搭建(四)
  5. Android
  6. Android上oprofile使用说明
  7. AnDroidDraw.apk的安装
  8. android中怎样将自己的应用程序的签名改为系统签名一样
  9. Eclipse上基于Android(安卓)SDK的开发

随机推荐

  1. Android HIDL学习(2) ---- HelloWorld
  2. 百度的Android招聘面试题
  3. Android 工具之hierarchyviewer
  4. Android客户端连接Struts2服务器,连接不上
  5. Android(安卓)Audio代码分析9 - AudioTra
  6. Android(安卓)Context getSystemService
  7. Android小项目之--电话与短信服务(附源码)
  8. 【多端应用开发系列1.1.1 —— Android:使
  9. 【笔记】Android上ROS开发介绍与安装简介
  10. Android(安卓)生成keystore,两种方式(转载