欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android app内部下载安装

程序员文章站 2022-04-01 10:45:36
// 3.下载成功,开始安装,兼容8.0安装位置来源的权限 public static void installApkO(Activity context, String downloadApkPath) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //是否有安装位置来源的权限 boolean haveInstallPermission = contex......
    // 3.下载成功,开始安装,兼容8.0安装位置来源的权限
    public static void installApkO(Activity context, String downloadApkPath) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //是否有安装位置来源的权限
            boolean haveInstallPermission = context.getPackageManager().canRequestPackageInstalls();
            if (haveInstallPermission) {
//                L.i("8.0手机已经拥有安装未知来源应用的权限,直接安装!");
                Utils.installApk(context, downloadApkPath);
            } else {
                Uri packageUri = Uri.parse("package:" + context.getPackageName());
                Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageUri);
                context.startActivityForResult(intent, 10086);
            }
        } else {
            Utils.installApk(context, downloadApkPath);
        }
    }

 

public static void installApk(Context context, String downloadApk) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        File file = new File(downloadApk);
//        L.i("安装路径=="+downloadApk);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri apkUri = FileProvider.getUriForFile(context, BaseApplication.getInstace().getApplication().getPackageName() + ".fileProvider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
        }
        context.startActivity(intent);

    }

需要配置

<provider
    android:name="com.tencent.bugly.beta.utils.BuglyFileProvider"
    android:authorities="${applicationId}.fileProvider"
    android:exported="false"
    android:grantUriPermissions="true"
    tools:replace="name,authorities,exported,grantUriPermissions">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"
        tools:replace="name,resource" />
</provider>

文件provider_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- /storage/emulated/0/Download/${applicationId}/.beta/apk-->
    <external-path name="beta_external_path" path="Download/"/>
    <!--/storage/emulated/0/Android/data/${applicationId}/files/apk/-->
    <external-path name="beta_external_files_path" path="Android/data/"/>
</paths>





点击链接加入群聊【编程之美】:https://jq.qq.com/?_wv=1027&k=9qYaAE3t

本文地址:https://blog.csdn.net/wan903531306/article/details/107185733