Skip to content

DangerousUtils.java #1384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Jasperben opened this issue Dec 3, 2020 · 1 comment
Open

DangerousUtils.java #1384

Jasperben opened this issue Dec 3, 2020 · 1 comment
Assignees
Labels

Comments

@Jasperben
Copy link

里面静默安装apk这方法失效了

@mao19951015
Copy link

这是我封装的,你可以看看

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import java.io.PrintWriter;

public class ApkInstaller {

private static final String TAG = "ApkInstaller";

/**
 * 安装apk,优先静默安装
 */
public static void install(String apkPath) {
    if (AppUtils.isAppRoot()) {
        //已root设备执行静默安装
        boolean silentSuccess = ApkInstaller.clientInstall(apkPath, AppUtils.getAppPackageName(), ActivityUtils.getLauncherActivity());
        if (!silentSuccess) {
            LogUtils.eTag(TAG, "静默安装失败");
            //静默安装未成功执行普通安装
            AppUtils.installApp(apkPath);
        }
        else {
            LogUtils.eTag(TAG, "静默安装成功");
            AppUtils.relaunchApp(true);
        }
    }
    else {
        //非root设备,普通弹窗安装
        AppUtils.installApp(apkPath);
    }
}

/**
 * 静默安装
 */
public static boolean clientInstall(String apkPath, String pkgName, String lunActName) {
    PrintWriter printWriter = null;
    Process process = null;
    try {
        process = Runtime.getRuntime().exec("su");
        printWriter = new PrintWriter(process.getOutputStream());
        printWriter.println("chmod 777 " + apkPath);
        printWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
        printWriter.println("pm install -r " + apkPath);
        printWriter.println("am start -n " + pkgName + "/" + lunActName);
        printWriter.flush();
        printWriter.println("exit");
        printWriter.flush();
        int value = process.waitFor();
        return value == 0;
    } catch (Exception e) {
        e.printStackTrace();
        LogUtils.eTag(TAG, "静默安装apk发生异常," + e.toString());
    } finally {
        if (process != null) {
            process.destroy();
        }
        if (printWriter != null) {
            printWriter.close();
        }
    }
    return false;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants