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()方法。
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。
转自:http://blog.csdn.net/sodino/article/details/6238818
分享到:
相关推荐
Android实现静默安装APK的两种方法.doc
android apk 的安装方式,看了这篇文档,希望对你有点帮助!
Android的静默安装似乎是一个很有趣很诱人的东西,但是,用普通做法,如果手机没有root权限的话,似乎很难实现静默安装,因为Android并不提供显示的Intent调用,一般是通过以下方式安装apk: Intent intent = new ...
使用Android5.0的环境编译的,不同版本之间,aidl文件可能会有差异,具体还需参照Android...详细参考我的博客文章《Android 无需root实现APK静默安装》:http://blog.csdn.net/yyh352091626/article/details/50533137
如果用过一些手机助手的朋友都知道,手机助手下载其他软件的更新程序后会自动静默更新。APK静默安装是指实现了在APK...压缩包里有两种实现apk静默安装的方法说明和详细的操作文档与源码。 涉及模块&技术 静默安装 ...
android 7.0 非root下 具有system权限下 实现静默安装 静默卸载 的多种实现方式
Android10.0 静默安装、卸载接口
android激活应用辅助功能,模拟点击静默安装apk,免root安装apk
实现android定时安装静默apk,自动在后台安装,安装完成后自动启动,需要root权限
在android4.4 平台,给预安装apk进行静默安装升级待命示例
通过AIDL调用Android源码静默安装卸载APK....
Android无需Root静默安装apk,无需获取root,下载文件包括Android工程slientInstall和Java打包工程Zip
通过系统root权限实现android系统的静默安装以及安装成功后自动重启
android apk静默安装方法,方法很实用,值得一看。
支持android5.1静默安装、卸载代码,封装在PackageInstaller模块内,在执行时可能需要修改部分framework代码使功能正常使用。
android实在获得root权限应用静默安装
主要介绍了Android实现静默安装的两种方法,一种是利用root权限静默安装实现,另一种是非root权限提示用户安装,两种方法分别给出了示例代码,需要的朋友可以参考借鉴,下面来一起看看吧。
Android10静默安装APK,卸载APP,需要系统权限,编译到priv-app下, 参考文章:http://t.csdn.cn/UrQQZ 部分API或者引用没有的可以去掉,不影响使用