如何以编程方式从本地存储安装.apk文件?

问题描述

我要安装以编程方式存储在本地存储中的.apk文件

这是我的代码

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

    builder.detectFileUriExposure();

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString() + "/Download/goodBox-1.0.apk")),"application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
    startActivity(intent);

但是此代码不起作用。

我的编译和目标SDK版本是30。

请帮助我。

解决方法

您的代码将不起作用,因为该代码仅适用于API级别23及以下。对于API级别24和更高级别,您应该遵循answer,并希望对您有所帮助。