Android 升级安装

本文介绍如何在Android 7.0及以上版本中实现应用间的文件共享,包括在AndroidManifest.xml中配置FileProvider,创建filepaths.xml文件指定共享路径,并提供了安装本地APK的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

内容来源自网络资源总结:
有关7.0之后应用间共享文件的详细适配,查看鸿洋大神博客Android 7.0 行为变更 通过FileProvider在应用间共享文件吧

升级包下载到本地之后,安装方法如下:

Build.VERSION.SDK_INT >= Build.VERSION_CODES.N需要在AndroidManifest.xml中添加provider适配

1、res下新建xml文件夹,并创建filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!--Environment.getExternalStorageDirectory().getAbsolutePath() + "/apk/"-->
    <files-path
        name="downloadDir"
        path="apk/" />
    <external-path
        name="downloadDir"
        path="apk/" />
</paths>

2、AndroidManifest.xml中添加provider标签

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>

3、调用系统安装

    public void installApk(String apkFileName) {
        try {
            String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/apk/";
            File apkfile = new File(dir + apkFileName);
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", apkfile);
            } else {
                uri = Uri.fromFile(apkfile);
            }
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值