Android 静默安装的几种方式
来源:互联网 发布:电视剧源码 编辑:程序博客网 时间:2024/05/21 10:45
@写在前面的话 Android 要想静默安装app,必须是系统应用或者具有Root权限,否则根本不可能实现静默安装 转载自@苍痕
1.系统API 。不是静默安装
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.parse("file://" + apkFilePath), "application/vnd.android.package-archive");
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- mContext.startActivity(intent);
2. 静默安装:利用ProcessBuilder
- /**
- * install slient
- *
- * @param filePath
- * @return 0 means normal, 1 means file not exist, 2 means other exception error
- */
- public static int installSilent(String filePath) {
- File file = new File(filePath);
- if (filePath == null || filePath.length() == 0 || file == null || file.length() <= 0 || !file.exists() || !file.isFile()) {
- return 1;
- }
- String[] args = { "pm", "install", "-r", filePath };
- ProcessBuilder processBuilder = new ProcessBuilder(args);
- Process process = null;
- BufferedReader successResult = null;
- BufferedReader errorResult = null;
- StringBuilder successMsg = new StringBuilder();
- StringBuilder errorMsg = new StringBuilder();
- int result;
- try {
- process = processBuilder.start();
- successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
- errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
- String s;
- while ((s = successResult.readLine()) != null) {
- successMsg.append(s);
- }
- while ((s = errorResult.readLine()) != null) {
- errorMsg.append(s);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (successResult != null) {
- successResult.close();
- }
- if (errorResult != null) {
- errorResult.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (process != null) {
- process.destroy();
- }
- }
- // TODO should add memory is not enough here
- if (successMsg.toString().contains("Success") || successMsg.toString().contains("success")) {
- result = 0;
- } else {
- result = 2;
- }
- Log.d("test-test", "successMsg:" + successMsg + ", ErrorMsg:" + errorMsg);
- return result;
- }
上面用到了反射调用,IPackageInstallObserver.class这个类在android sdk里面是没有的,您需要下载android_dependency.jar放到你工程的libs目录,这个jar提供了与PackageManager反射调用相关的类的定义。
注意:静默安装还需要在你的AndroidManifest.xml中添加权限声明。该权限默认赋予系统应用,第三方应用即使声明了,也拿不到该权限!
/** * 执行具体的静默安装逻辑,需要手机ROOT。 * @param apkPath * 要安装的apk文件的路径 * @return 安装成功返回true,安装失败返回false。 */public boolean install(String apkPath) { boolean result = false; DataOutputStream dataOutputStream = null; BufferedReader errorStream = null; try { // 申请su权限 Process process = Runtime.getRuntime().exec("su"); dataOutputStream = new DataOutputStream(process.getOutputStream()); // 执行pm install命令 String command = "pm install -r " + apkPath + "\n"; dataOutputStream.write(command.getBytes(Charset.forName("utf-8"))); dataOutputStream.flush(); dataOutputStream.writeBytes("exit\n"); dataOutputStream.flush(); process.waitFor(); errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream())); String msg = ""; String line; // 读取命令的执行结果 while ((line = errorStream.readLine()) != null) { msg += line; } Log.d("TAG", "install msg is " + msg); // 如果执行结果中包含Failure字样就认为是安装失败,否则就认为安装成功 if (!msg.contains("Failure")) { result = true; } } catch (Exception e) { Log.e("TAG", e.getMessage(), e); } finally { try { if (dataOutputStream != null) { dataOutputStream.close(); } if (errorStream != null) { errorStream.close(); } } catch (IOException e) { Log.e("TAG", e.getMessage(), e); } } return result;}
2 0
- Android 静默安装的几种方式
- Android静默安装的几种方法
- 静默安装的方式
- 静默安装的两种实现方式
- android的静默安装
- Android的静默安装
- Android 安装应用的两种方式--外部应用安装器安装和静默安装(系统应用)
- android如何静默方式来安装apk
- android如何静默方式来安装apk
- android如何静默方式来安装apk
- android静默方式来安装apk
- android如何静默方式来安装apk
- android如何静默方式来安装apk
- Android静默安装 root提权方式
- Android静默安装的实现
- android静默安装的实现
- android静默安装的实现
- Android应用的静默安装
- 数组的相关运算(sizeof和strlen的灵活应用)
- ubuntu切换多终端的方法
- 结构体与char型的转换
- ZOJ
- 华为在线编程系列-删除字符串中出现次数最少的字符
- Android 静默安装的几种方式
- Linux GCC与GDB调试
- java基础----final与匿名内部类
- 机器学习算法及代码实现--决策树
- 廖雪峰老师——Python进阶( 模块 )
- 功夫传人(天梯赛)
- 计算机网络知识点(做题笔记)
- 关于make modules 和 make modules_install 作用的概念
- JAVA-4.2-API帮助文档、String类、StringBuffer和StringBuilder