安卓个版本安装app

安卓个版本安装app

6.0之前安装app

private boolean startInstall(Context context, Uri uri) {
        File apkFile = new File(uri.getPath());
        if (!apkFile.exists()) {
            Log.e("IDownloadService", " local file has been deleted! ");
            return false;
        }


        Intent intent = new Intent();
        //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        //判断是否是AndroidN以及更高的版本
      
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(apkFile), 
         "application/vnd.android.package-archive");
      
        context.startActivity(intent);
        return true;
    }

7.0安装app

private boolean startInstall(Context context, Uri uri) {
        File apkFile = new File(uri.getPath());
        if (!apkFile.exists()) {
            Log.e("IDownloadService", " local file has been deleted! ");
            return false;
        }


        Intent intent = new Intent();
        //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        //判断是否是AndroidN以及更高的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, Config.APPLICATION_ID + ".fileProvider", apkFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        }

        context.startActivity(intent);
        return true;
    }

在 xml 添加 file_paths.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path path="Android/data/com.eventmosh.evente/" name="files_root" />
    <external-path path="." name="external_storage_root" />
</paths>

manifests 配置

 <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>

8.0安装app

添加权限


<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,634评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,128评论 2 59
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • 变通的思想超级重要,让很多不可能的事情成为可能。
    斐丽希娅阅读 133评论 0 0
  • 不得不承认,女人过了三十岁,就不太愿意提起生日啊,年龄啊这类东西。而从今天开始,我就三十二岁了。人说三十而立。现在...
    林贝拉阅读 284评论 0 0