Android中如何实现APP静默安装(记录)

首先,APP静默安装是需要root权限的。

如何判断设备是否有root权限?

/**
 * 设备是否已root
 *
 * @return
 */
private boolean isRoot() {
    if(new File("/system/bin/su").exists() || new File("/system/xbin/su").exists()){
        return true;
    }
    return false;
}

如何实现静默安装?

/**
 * 静默安装工具类
 */
public class InstallUtils {

    public static final String TAG = InstallUtils.class.getSimpleName();
    public static final int APP_INSTALL_AUTO = 0;
    public static final int APP_INSTALL_INTERNAL = 1;
    public static final int APP_INSTALL_EXTERNAL = 2;

    /**
     * root静默安装
     * @param filePath
     * @param mContext
     * @param packageManager
     */
    public static void installSilent(String filePath, Context mContext, PackageManager packageManager){
        installSilent(filePath,"-r " + getInstallLocationParams(), mContext, packageManager);
    }

    /**
     * root静默安装
     * @param filePath
     * @param pmParams
     */
    private static void installSilent(String filePath, String pmParams, Context mContext, PackageManager packageManager){
        if (filePath == null || filePath.length() == 0) {
            Log.i(TAG, "installSilent: error path");
            return;
        }

        File file = new File(filePath);
        if (file.length() <= 0 || !file.exists() || !file.isFile()) {
            Log.i(TAG, "installSilent:  error file");
            return;
        }
        //LD_LIBRARY_PATH 指定链接库位置 指定安装命令
        String command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install " +
                (pmParams == null ? "" : pmParams) +
                " " +
                filePath.replace(" ", "\\ ");
        //以root模式执行
        try {
            ShellUtils.CommandResult result = ShellUtils.execCommand(command, true, true);
            if (result.successMsg != null
                    && (result.successMsg.contains("Success") || result.successMsg.contains("success"))) {
                Log.i(TAG, "installSilent: success");
                //删除安装包
                FileManager.deleteDir(TXTManager.apkPath);
                //隐式启动APP
                Intent intent = packageManager.getLaunchIntentForPackage("com.deepreality.advertising_app");
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                mContext.startActivity(intent);
                //APP关闭
                appExit(mContext);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * root 静默卸载
     * @param packageName
     * @param isKeepData
     */
    public static void uninstallSilent(String packageName, boolean isKeepData){
        if (packageName == null) {
            Log.i(TAG, "uninstallSilent: error package");
            return;
        }
        String command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm uninstall" +
                (isKeepData ? " -k " : " ") +
                packageName.replace(" ", "\\ ");
        ShellUtils.CommandResult result = ShellUtils.execCommand(command, true, true);
        if (result.successMsg != null
                && (result.successMsg.contains("Success") || result.successMsg.contains("success"))) {
            Log.i(TAG, "uninstallSilent: success");
        }
    }

    private static String getInstallLocationParams() {
        int location = getInstallLocation();
        switch (location) {
            case APP_INSTALL_INTERNAL:
                return "-f";
            case APP_INSTALL_EXTERNAL:
                return "-s";
            default:
                break;
        }
        return "";
    }

    public static int getInstallLocation() {
        ShellUtils.CommandResult commandResult = ShellUtils.execCommand("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm get-install-location",
                false, true);
        if (commandResult.result == 0 && commandResult.successMsg != null && commandResult.successMsg.length() > 0) {
            try {
                int location = Integer.parseInt(commandResult.successMsg.substring(0, 1));
                switch (location) {
                    case APP_INSTALL_INTERNAL:
                        return APP_INSTALL_INTERNAL;
                    case APP_INSTALL_EXTERNAL:
                        return APP_INSTALL_EXTERNAL;
                    default:
                        break;
                }
            } catch (NumberFormatException e) {
                Log.d(TAG, "pm get-install-location error!!!  NumberFormatException :", e);
            }
        }
        return APP_INSTALL_AUTO;
    }

    /**
     * 关闭APP
     *
     * @param mContext
     */
    private static void appExit(Context mContext) {
        android.app.ActivityManager activityManager = (android.app.ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        activityManager.killBackgroundProcesses(mContext.getPackageName());
        System.exit(0);
    }
}

你可能感兴趣的:(Android开发)