Android安装APK

1.AndroidManifest权限设置

<!-- android8.0安装APK必备权限 -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

2.创建 android7.0 文件提供者

  1. 在res文件夹下创建xml文件夹
  2. 在xml文件夹下创建 file_paths.xml 文件 文件内容如下:
    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        
        <!-- 代表设备的根目录 new File("/") -->
        <root-path name="root" path="" />
    
        <!-- 代表 context.getFileDir() -->
        <files-path name="files" path="" />
    
        <!-- 代表 context.getCacheDir() -->
        <cache-path name="cache" path="" />
    
        <!-- 代表 Environment.getExternalStorageDirectory() -->
        <external-path name="external" path="" />
    
        <!-- 代表 context.getExternalFilesDirs() -->
        <external-files-path name="name" path="path" />
    
        <!-- 代表 getExternalCacheDirs() -->
        <external-cache-path name="name" path="path" />
    </paths>
    <external-path name="external" path="pics"/>

    代表的目录即为:Environment.getExternalStorageDirectory()/pics 

  3. 在AndroidManifest.xml添加文件提供者
            <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="你的包名.fileProvider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths" />
            </provider>

     

  4. 放大g
  5. gy
  6.  

3.正式安装

    private void detectionVersions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //获取是否有权限
            boolean jurisdiction = getPackageManager().canRequestPackageInstalls();
            if (jurisdiction) {
                //开始安装
                installApk();
            } else {
                //没有权限,申请权限
                ActivityCompat.requestPermissions((Activity) MainActivity.this, new String[]{android.Manifest.permission.REQUEST_INSTALL_PACKAGES}, 3);
            }
        } else {
            //开始安装
            installApk();
        }
    }

 

    //安装应用
    public void installApk(File binaryFile) {
        //判断版本大于等于7.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            String applicationId = getPackageName() + ".fileProvider";
            Uri uri = FileProvider.getUriForFile(MainActivity.this, applicationId, binaryFile);
            //给目标应用一个临时授权
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            startActivity(intent);
        } else {
            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(binaryFile);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            startActivity(intent);
        }
    }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值