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()方法。
- 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的手机,一般上是不支持的。
以上这两种方法都在AndroidManifest中声明android.permission.INSTALL_PACKAGES,有一点比较奇怪的是执行“ int result = checkCallingOrSelfPermission(Intent.ACTION_PACKAGE_INSTALL) ”,result的值为android.content.pm.PackageManager.PERMISSION_DENIED而不是PERMISSION_GRANTED。