繁体   English   中英

以编程方式安装下载的apk

[英]install downloaded apk programmatically

我搜索堆栈并找到以下安装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
}

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

     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();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM