1、正常安装
public static void reqSystemInstall(Context context, String packagePath) { if (TextUtils.isEmpty(packagePath)) return; File targetFile = new File(packagePath); if (!targetFile.exists() || targetFile.isDirectory()) return; try { Uri packageURI = Uri.fromFile(targetFile); Intent intent = null; if (Build.VERSION.SDK_INT >= 14) { intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true); intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true); intent.setData(packageURI); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(packageURI, "application/vnd.android.package-archive"); } if(context instanceof Activity) ((Activity) context).startActivityForResult(intent, 0); else { intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } } catch (ActivityNotFoundException anfe) { } }2、打开 public static void reqSystemOpen(Context context, String pkgName) { PackageManager pm = context.getPackageManager(); try { Intent intent = pm.getLaunchIntentForPackage(pkgName); if (intent != null) { intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } } catch (Exception e) { } } 3、获取已安装程序(含签名) private boolean isAvilible(Context context, String packageName) { PackageManager packageManager = context.getPackageManager(); List<PackageInfo> packageInfos = packageManager.getInstalledPackages(PackageManager.GET_SIGNATURES); List<String> packageNames = new ArrayList<String>(); // 从pinfo中将包名字逐一取出,压入pNameList中 if (packageInfos != null) { for (int i = 0; i < packageInfos.size(); i++) { String packName = packageInfos.get(i).packageName; packageNames.add(packName); } } return packageNames.contains(packageName); }4、静默安装本质就是执行pm命令进行安装,是否有root、安装权限。
在AndroidManifest.xml添加权限:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />是否有root private final static int kSystemRootStateUnknow = -1; private final static int kSystemRootStateDisable = 0; private final static int kSystemRootStateEnable = 1; private static int systemRootState = kSystemRootStateUnknow; public static boolean isPhoneRoot() { if (systemRootState == kSystemRootStateEnable) { return true; } else if (systemRootState == kSystemRootStateDisable) { return false; } File f = null; final String kSuSearchPaths[] = { "/system/bin/", "/system/xbin/", "/system/sbin/", "/sbin/", "/vendor/bin/" }; try { for (int i = 0; i < kSuSearchPaths.length; i++) { f = new File(kSuSearchPaths[i] + "su"); if (f != null && f.exists()) { systemRootState = kSystemRootStateEnable; return true; } } } catch (Exception e) { } systemRootState = kSystemRootStateDisable; return false; }执行pm命令(这个类可当做执行命令的工具来用) import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.List; public class ShellUtils { public static final String COMMAND_SH = "sh"; private static final String COMMAND_SU = "su"; private static final String COMMAND_EXIT = "exit\n"; private static final String COMMAND_LINE_END = "\n"; public static CommandResult execCommand(String command) { return execCommand(new String[] { command }, true); } public static CommandResult execCommand(List<String> commands) { return execCommand(commands == null ? null : commands.toArray(new String[] {}), true); } public static CommandResult execCommand(String[] commands) { return execCommand(commands, true); } public static CommandResult execCommand(String command, boolean isNeedResultMsg) { return execCommand(new String[] { command }, isNeedResultMsg); } public static CommandResult execCommand(List<String> commands, boolean isNeedResultMsg) { return execCommand(commands == null ? null : commands.toArray(new String[] {}), isNeedResultMsg); } public static CommandResult execCommand(String[] commands, boolean isNeedResultMsg) { return doExecCommand(COMMAND_SH, commands, isNeedResultMsg); } public static CommandResult execSuperUserCommand(String command) { return execSuperUserCommand(new String[] { command }, true); } public static CommandResult execSuperUserCommand(String[] commands, boolean isNeedResultMsg) { return doExecCommand(COMMAND_SU, commands, isNeedResultMsg); } private static CommandResult doExecCommand(String shell, String[] commands, boolean isNeedResultMsg) { int result = -1; if (commands == null || commands.length == 0) { return new CommandResult(result, null, null); } Process process = null; StringBuilder successMsg = null; StringBuilder errorMsg = null; InputStream in = null; InputStream error = null; OutputStream out = null; try { process = Runtime.getRuntime().exec(shell); out = process.getOutputStream(); DataOutputStream os = new DataOutputStream(out); for (String command : commands) { if (command == null) { continue; } 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) { in = process.getInputStream(); error = process.getErrorStream(); successMsg = new StringBuilder(); errorMsg = new StringBuilder(); BufferedReader successResult = new BufferedReader(new InputStreamReader(in)); BufferedReader errorResult = new BufferedReader(new InputStreamReader(error)); String s; while ((s = successResult.readLine()) != null) { successMsg.append(s); } while ((s = errorResult.readLine()) != null) { errorMsg.append(s); } } } catch (Exception e) { } finally { try { if (in != null) in.close(); if (out != null) out.close(); if (error != null) error.close(); } catch (IOException e) { } if (process != null) process.destroy(); } return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null : errorMsg.toString()); } public static class CommandResult { public int result; public String successMsg; 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; } } }