以编程方式安装下载的apk

时间:2017-10-24 09:58:01

标签: java android apk

我搜索到堆栈并找到以下方法安装apk但它在Android API 6及更高版本上无法正常工作。当此方法运行时,此错误在移动屏幕上出现

  

"解析包裹有问题"

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(PathManagers.getApk(context) + zipFileNameUnzipApk));
intent.setDataAndType(uri, "application/vnd.android.package-archive");

if (intent.resolveActivityInfo(context.getPackageManager(), 0) != null)
{
    context.startActivity(intent);
}
else
{

    // if you reach this place, it means there is no any file
    // explorer app installed on your device
}

1 个答案:

答案 0 :(得分:0)

如果您的设备已植根,请使用此方法

     private void installApk(String filename) {
    File file = new File(filename);
    if (file.exists()) {
        try {
            //final String command = " pm install -r " + file.getAbsolutePath();
            final String command = "pm install -r " + file.getAbsolutePath();

            //Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
            Process proc = Runtime.getRuntime().exec(command);
            proc.waitFor();
        } catch (Exception e) {
            ProjectUtils.PSLogger(TAG, "error while installation of apk");
            e.printStackTrace();
        }
    }
}