Android app内部下载安装

编程知识 更新时间:2023-05-02 19:38:20
    // 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/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/?_wv=1027&k=9qYaAE3t

更多推荐

Android app内部下载安装

本文发布于:2023-04-28 08:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/a4c185cac17763e829ccbc637c354a1b.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 108085文章数
  • 27321阅读数
  • 0评论数