android静默安装与静默卸载
来源:互联网 发布:小学四年级体测数据 编辑:程序博客网 时间:2024/05/18 01:13
android5.0和6.0静默安装与卸载
android5.0使用shell命令实现静默安装和卸载
废话不多说直接上代码
//静默安装 private void installSpecific(String path) {//apk存储路径 ShellUtils.execCommand("chmod 777 " + path, false); ShellUtils.CommandResult res = ShellUtils.execCommand("pm install -r " + pkg.getFilePath(), false);//静默安装的shell命令 ShellUtils.CommandResult result = ShellUtils.execCommand("pm uninstall " + "包名", false);//静默卸载的shell命令 if (res.result == 0 && !TextUtils.isEmpty(res.successMsg) && res.successMsg.equals("Success")) { //安装成功 } else { //安装失败 } }
ShellUtils工具类代码
public class ShellUtils { public static final String COMMAND_SU = "can"; public static final String COMMAND_SH = "sh"; public static final String COMMAND_EXIT = "exit\n"; public static final String COMMAND_LINE_END = "\n"; private ShellUtils() { throw new AssertionError(); } /** * check whether has root permission * * @return */ public static boolean checkRootPermission() { return execCommand("echo root", true, false).result == 0; } /** * execute shell command, default return result msg * * @param command * command * @param isRoot * whether need to run with root * @return * @see ShellUtils#execCommand(String[], boolean, boolean) */ public static CommandResult execCommand(String command, boolean isRoot) { return execCommand(new String[] { command }, isRoot, true); } /** * execute shell commands, default return result msg * * @param commands * command list * @param isRoot * whether need to run with root * @return * @see ShellUtils#execCommand(String[], boolean, boolean) */ public static CommandResult execCommand(List<String> commands, boolean isRoot) { return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, true); } /** * execute shell commands, default return result msg * * @param commands * command array * @param isRoot * whether need to run with root * @return * @see ShellUtils#execCommand(String[], boolean, boolean) */ public static CommandResult execCommand(String[] commands, boolean isRoot) { return execCommand(commands, isRoot, true); } /** * execute shell command * * @param command * command * @param isRoot * whether need to run with root * @param isNeedResultMsg * whether need result msg * @return * @see ShellUtils#execCommand(String[], boolean, boolean) */ public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) { return execCommand(new String[] { command }, isRoot, isNeedResultMsg); } /** * execute shell commands * * @param commands * command list * @param isRoot * whether need to run with root * @param isNeedResultMsg * whether need result msg * @return * @see ShellUtils#execCommand(String[], boolean, boolean) */ public static CommandResult execCommand(List<String> commands, boolean isRoot, boolean isNeedResultMsg) { return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, isNeedResultMsg); } /** * execute shell commands * * @param commands * command array * @param isRoot * whether need to run with root * @param isNeedResultMsg * whether need result msg * @return <ul> * <li>if isNeedResultMsg is false, {@link CommandResult#successMsg} * is null and {@link CommandResult#errorMsg} is null.</li> * <li>if {@link CommandResult#result} is -1, there maybe some * excepiton.</li> * </ul> */ public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) { int result = -1; if (commands == null || commands.length == 0) { return new CommandResult(result, null, null); } Process process = null; BufferedReader successResult = null; BufferedReader errorResult = null; StringBuilder successMsg = null; StringBuilder errorMsg = null; DataOutputStream os = null; try { process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH); os = new DataOutputStream(process.getOutputStream()); for (String command : commands) { if (command == null) { continue; } // donnot use os.writeBytes(commmand), avoid chinese charset // error os.write(command.getBytes()); os.writeBytes(COMMAND_LINE_END); os.flush(); } os.writeBytes(COMMAND_EXIT); os.flush(); result = process.waitFor(); // get command result if (isNeedResultMsg) { successMsg = new StringBuilder(); errorMsg = new StringBuilder(); 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 (os != null) { os.close(); } if (successResult != null) { successResult.close(); } if (errorResult != null) { errorResult.close(); } } catch (IOException e) { e.printStackTrace(); } if (process != null) { process.destroy(); } } return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null : errorMsg.toString()); } /** * result of command * <ul> * <li>{@link CommandResult#result} means result of command, 0 means normal, * else means error, same to excute in linux shell</li> * <li>{@link CommandResult#successMsg} means success message of command * result</li> * <li>{@link CommandResult#errorMsg} means error message of command result</li> * </ul> * * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> * 2013-5-16 */ public static class CommandResult { /** result of command **/ public int result; /** success message of command result **/ public String successMsg; /** error message of command result **/ public String errorMsg; public CommandResult(int result) { this.result = result; } public CommandResult(int result, String successMsg, String errorMsg) { this.result = result; this.successMsg = successMsg; this.errorMsg = errorMsg; } }}
android6.0可以使用shell命令进行静默安装,但是静默卸载得用反射
我们直接把IPackageDeleteObserver和.IPackageInstallObserver类复制出来放到我们的工程中,注意包名要和系统文件包名android.content.pm保持一致,然后使用反射静默卸载,上代码
PackageManager pm = mContext.getPackageManager(); Method[] methods = pm != null ? pm.getClass().getDeclaredMethods() : null; Method mDel = null; if (methods != null && methods.length > 0) { for (Method method : methods) { if (method.getName().toString().equals("deletePackage")) { mDel = method; break; } } } if (mDel != null) { mDel.setAccessible(true); mDel.invoke(pm, pkg.getPackageName(), new IPackageDeleteObserver.Stub() { @Override public void packageDeleted(String packageName, int returnCode) throws RemoteException { Log.i("appstore", "del apk >>" + packageName + ";code >>" + returnCode); if (!pkg.getPackageName().equals(packageName)) { return; } if (returnCode != 0) { ToastUtils.show(mContext, pkg.getAppName() + mContext.getString(R.string .uninstall_success)); } else { ToastUtils.show(mContext, pkg.getAppName() + mContext.getString(R.string .install_success)); } } }, 0);
添加系统权限
在系统上安装程序是需要注册权限的,在Manifest文件中加入如下语句:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
由于然后同样是manifest文件,在节点中加入sharedUserId属性:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.slienceinstall" android:sharedUserId="android.uid.system" //声明为系统应用 android:versionCode="1" android:versionName="1.0" > ... ...<manifest/>
必须得有系统签名,应用的静默安装和卸载才能成功
阅读全文
0 0
- android静默安装与静默卸载
- WINDOWS 下静默安装 与 静默卸载
- android静默安装、静默卸载apk方法
- Android静默安装和静默卸载
- Android--静默安装和静默卸载
- Android静默安装和静默卸载
- Android静默安装和静默卸载
- android实现静默卸载和静默安装
- Android静默安装和静默卸载
- Android静默安装和静默卸载
- Android实现静默安装与卸载
- Android实现静默安装与卸载
- Android实现静默安装与卸载
- Android实现静默安装与卸载
- Android 静默安装与卸载分析
- android 实现静默安装、卸载
- android 实现静默安装、卸载
- android 实现静默安装、卸载
- 【JavaSE】接口和抽象类
- 把char*转换为wchar_t*
- Linux上使用tomcat8上传图片,通过nginx无法访问
- redis-3.2.8-Sentinel集群安装和部署
- 在研究VUEX的过程中的一个范例
- android静默安装与静默卸载
- MKT USB OTG功能如何打开及实现
- Laravel 常用命令合集
- 测试onmouseover事件
- 【教程】树莓派安装OS之后的初始配置,以安装OpenCV 3.1.0为例
- 想成为并做好一个IT项目经理,你需要坚持做的事情
- 深度学习所需的python-学习笔记10
- uiautomator中相同控件名该如何获取想要的控件,instance(0)区分布局一样的控件
- Hadoop中的文件格式