美文网首页
安卓个版本安装app

安卓个版本安装app

作者: lixinxin | 来源:发表于2018-10-10 10:28 被阅读11次

    安卓个版本安装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"/>
    

    相关文章

      网友评论

          本文标题:安卓个版本安装app

          本文链接:https://www.haomeiwen.com/subject/gpxpaftx.html