Android7.0机型适配

###Android7.0机型适配存储目录的共享
都知道如今AI都很火,AI一定会诞生一些下一代巨头企业,各行各业都争抢这一股风,百度CEO不扔下了公司的内务无论,一头扎进了AI的研究团队工做中,前段时间李彦宏驾着本身公司生产的无人驾驶汽车,就是6,各个大巨头都在AI方面有重要项目在实施,都争抢着下一个时代的山头。在物联网方面,作的不错的是小米,小米的生态链,汇集了70多家智能硬件公司,造成了小米生态链,将来的小米将会在物联网将是一个大赢家,咱们知道的小米路由器,小米手环,小米扫地机器人等都是这70多家公司中来生产和研发的,雷军太有远见了。好了,不闲说了,进入正题,记录一下我吗7.0版本对使用存储的要求。android


最近根据需求作了在线更新app问题,起初没有对7.0以上的机型进行
适配,形成7.0手机没法跳转到app安装界面。
   说一下吧,android系统发展愈来愈好用,用户体验愈来愈好,对
用户的隐私也进行了必定规范性的保护,咱们都知道从6.0系统的开始
goole就开始对app使用手机权限进行了必定的约束,对于6.0以上 
的权限申请我就很少说了,对于权限申请,本身写了一个框架,在项
目中使用很方便。下面咱们就说说7.0系统对存储使用增长
了“FileProvider”,咱们来看看使用方式,阅读一下官方文档。复制代码

#####FileProvider介绍安全

FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file:/// Uri.
A content URI allows you to grant read and write access using temporary access permissions. When
you create an Intent containing a contentURI, in
order to send the content URI to a client app, you can also call Intent.setFlags() to add permissions. These permissions are available to the client app for as long as the stack for a receiving Activity is active. For an Intent going to a Service, the permissions are available as long as the Service is running.
bash

In comparison, to control access to a file:/// Uri you have to modify the file system permissions of the underlying file. The permissions you provide become available to any app, and remain in effect until you change them. This level of access is fundamentally insecure.app

The increased level of file access security offered by a content URI makes FileProvider a key part of Android's security infrastructure.框架

This overview of FileProvider includes the following topics:dom

意思就是说

FileProvider是一个特殊的ContentProvider子类,它经过为文件建立一个Content:/ / Uri而不是file:/ / Uri,从而促进与应用程序关联的文件的安全共享。
ide

内容URI容许您使用临时访问权限授予读和写访问。当您建立包含内容URI的意图时,为了将内容URI发送到客户机应用程序,您还能够调用intent. setflags()来添加权限。只要接收活动的堆栈是活动的,客户机应用程序就可使用这些权限。为了得到服务,只要服务运行,权限就可用。ui

相比之下,为了控制对文件的file:/ / / Uri,您必须修改底层文件的文件系统权限。您提供的权限对任何应用程序均可用,而且一直有效,直到您更改它们为止。这种级别的访问基本上是不安全的。spa

由内容URI提供的文件访问安全性的提升使得FileProvider成为Android安全基础设施的关键部分。
code

###使用方式
1.在程序清单文件中添加代码:

<manifest>
...
<application>
   ...
   <provider  android:name= "android.support.v4.content.FileProvid"  
        android:authorities="com.mydomain.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
                   <!--元数据-->
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_path" />
    </provider>
    ...
</application>
</manifest>复制代码

com.mydomain更换成本身项目的包名,惟一性。

2.在res目录下新建一个xml文件夹,文件下新建一个xml文件“file_path”

3.file_path文件写法

<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
    <root-path name="download" path=""/>
</paths>
</resources>复制代码

根据咱们使用的存储路径不一样,对第三层的配置也不同。

(1)当咱们使用Context.getFilesDir()获取路径时咱们应该配
 置成 <files-path name="name" path="path" />
 (2)当咱们使用getCacheDir()获取存储路径时,应该配置成
 <cache-path name="name" path="path" />
 (3)使用Environment.getExternalStorageDirectory()
 获取目录时,<root-path name="download" path=""/>,path值能够为null,null表明受权这个目录下的全部文件,name必须写,否则会报严重异常
 (4)使用Context.getExternalCacheDir()
 <external-cache-path name="name" path="path" />
 (5)使用Context.getExternalFilesDir(null)配置成
 <external-files-path name="name" path="path" />复制代码

另外,咱们在跳转到安装目录代码有变以下:

public static void install(Context context,String target) {
    File file = new File(target);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    // 因为没有在Activity环境下启动Activity,设置下面的标签
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if(Build.VERSION.SDK_INT>=24) { //判读版本是否在7.0以上
        //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致   参数3  共享的文件
        Uri apkUri =
                FileProvider.getUriForFile(context, "com.gzzhe.zhhwlkj.zigou.fileprovider", file);
        //添加这一句表示对目标应用临时受权该Uri所表明的文件
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
    }else{
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
    }
    context.startActivity(intent);
}复制代码

好了就介绍到这儿吧。

相关文章
相关标签/搜索