android安装、卸载、打开Apk 适配7.0

来源:互联网 发布:qq采集软件哪款好用 编辑:程序博客网 时间:2024/06/06 20:39

  除了android安装apk需要进行适配。打开和卸载都不需要进行适配,也没有很多要讲的,代码都很简单,都是去启动一个Activity、、、

一、安装apk

  随着android版本的升级,android对隐私的保护力度越来越大。

  比如: android6.0引入了动态权限控制(Runtime Permissons)。android7.0又引入了私有目录被限制访问(StrctMode)Api政策

  由于android7.0私有目录被限制访问,在android7.0安装apk时,会报出FileUriExposedException异常

  使用FileProvider来解决android7.0私有目录被限制访问的问题

  使用FileProvider的步骤

  1.在AndroidManifest.xml清单文件中注册provider,因为provider也是四大组件之一,跟ContentProvider组件功能类似,都是向外提供数据。都是在Application节点下

<provider        android:name="android.support.v4.content.FileProvider"        android:authorities="com.app.rui.appstore.fileProvider"        android:exported="false"        android:grantUriPermissions="true">        <!-- 元数据 -->        <meta-data               android:name="android.support.FILE_PROVIDER_PATHS"               android:resource="@xml/file_paths"/></provider>

  注意:
    exported: 必须为false,为true则会报安全异常
    grantUriPermissions: 为true表示授予URI临时访问权限。
    authorities: 组件标识,跟ContentProvider一样,为了唯一标识,一般 包名+”.fileProvider”
    resourece: 指定res下的xml配置文件”@xml/file_paths”注意必须在xml目录下,文件名可以自定义

  2.创建resource指定的目录和xml

这里写图片描述

<paths>    <external-path path="Download" name="download"/></paths>
<files-path/> 代表的根目录: Context.getFiesDir()<external-path/> 代表的根目录:Environment.getExternalStorageDirectory()<cache-path/> 代表的根目录:Context.getCacheDir()

  注: path = “” 指定你可以访问根目录及其子目录

  比如: path = “Download”,代表你可以访问/storage/emulated/0/Download目录,及子目录

  3.使用FileProvider获取Uri

    public static boolean installApk(Context context, String filePath) {        File file = new File(filePath);        if (!file.exists() || !file.isFile() || file.length() <= 0) {            return false;        }        Intent intent = new Intent(Intent.ACTION_VIEW);        // 由于没有在Activity环境下启动Activity,设置下面的标签        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        Uri apkUri = null;        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){            apkUri = FileProvider.getUriForFile(context, "com.app.rui.appstore.fileProvider", file);            //添加这一句表示对目标应用临时授权该Uri所代表的文件            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);        } else {            apkUri = Uri.fromFile(file);        }        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");        context.startActivity(intent);        return true;    }
注意:intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);这句一定要在new Intent下,不然会出错FileProvider.getUriForFile(Context  context, String authority, File file)authority参数其实就是在androidManfist.xml中<provider/>节点下的android:authorities的值

这里写图片描述

  到此就适配了7.0和7.0以下android安装apk的代码

二、打开已安装的app

    public static void runApp(Context context, String packagename) {        context.startActivity(new Intent(context.getPackageManager().getLaunchIntentForPackage(packagename)));    }

三、卸载第三方app,如果是系统app,需要获取Root权限,暂时不做介绍

    public static boolean uninstallApk(Context context, String packageName) {        if (TextUtils.isEmpty(packageName)) {            return false;        }        Intent i = new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + packageName));        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        context.startActivity(i);        return true;    }