这是一个系列,我们将其命名为工具箱,如果你还没有看之前的文章:

Android工具箱之Context解析

Android工具箱之文件目录

Android工具箱之组织你的代码文件

Android工具箱之理解app资源文件

Android工具箱之Activity生命周期

Android工具箱之迁移到AppCompat

概述

默认情况下,一个Android app是不会获取任何权限的。当你的app需要使用任何被设备保护起来的特性的时候(比如发送网络请求,访问照相机,发送短信等),其必须从用户那儿获取到相关的权限。

Android 6.0之前的权限管理

在Android6.0之前,开发者获取设备权限非常的简单,所有的权限会在安装的时候处理。比如一个用户想要安装一个app,那么在安装之前,Android系统会弹出一个对话框,让你能够浏览该应用获取你设备的哪些权限,比如Dropbox:

对于开发者而言,如果你想获取权限,只需要在AndroidManifest.xml文件中申明。举个栗子,获取联系人权限:

            ...

用户在安装完以后没有任何更改权限的机会。虽然让开发者更方便的处理权限,但是对于用户来说,是个不好的体验。

Android 6.0的权限管理

最显著的改变是Android 6.0支持用户随时的更改app权限,随时的含义包括app正在运行。6.0的设备会分两种权限。

常规权限

何为常规权限,即你只需要在xml中申明,用户不会弹出任何让其选择是否开启权限的弹窗,即静默权限。这些权限包括:网络访问,获取网络状态等。注意:常规的权限,你必须要在xml中申明。

            ...

运行时权限

如果你想获取的权限不在常规权限列表中,你需要进行运行时权限处理。即你需要在代码中动态的获取权限,系统会弹出对话框供用户选择:

那么如何定义运行权限呢?其实和常规权限是一样的,你需要首先在xml中申明:

        ...

接下来,你需要申请权限,然后处理结果。下面的代码将会为你展示这些步骤:

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                // In an actual app, you'd want to request a permission when the user performs an action        // that requires that permission.        getPermissionToReadUserContacts();    }    // Identifier for the permission request    private static final int READ_CONTACTS_PERMISSIONS_REQUEST = 1;    // Called when the user is performing an action which requires the app to read the    // user's contacts    public void getPermissionToReadUserContacts() {        // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid        // checking the build version since Context.checkSelfPermission(...) is only available        // in Marshmallow        // 2) Always check for permission (even if permission has already been granted)        // since the user can revoke permissions at any time through Settings        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)                != PackageManager.PERMISSION_GRANTED) {            // The permission is NOT already granted.            // Check if the user has been asked about this permission already and denied            // it. If so, we want to give more explanation about why the permission is needed.            if (shouldShowRequestPermissionRationale(                    Manifest.permission.READ_CONTACTS)) {                // Show our own UI to explain to the user why we need to read the contacts                // before actually requesting the permission and showing the default UI            }            // Fire off an async request to actually get the permission            // This will show the standard permission request dialog UI            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},                    READ_CONTACTS_PERMISSIONS_REQUEST);        }    }    // Callback with the request from calling requestPermissions(...)    @Override    public void onRequestPermissionsResult(int requestCode,                                           @NonNull String permissions[],                                           @NonNull int[] grantResults) {        // Make sure it's our original READ_CONTACTS request        if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) {            if (grantResults.length == 1 &&                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {                Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();            } else {                Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show();            }        } else {            super.onRequestPermissionsResult(requestCode, permissions, grantResults);        }    }}

权限Groups

当一个app需要大量的权限的时候,如果让开发者每次需要使用权限而弹出对话框,显示是权限的滥用。而权限群允许app在某一时间点同时申请多个权限。但是注意,大多数情况下,你还是应该单独的去获取单一的权限,但是比如下面这个栗子:当你需要获取联系人读的权限,你向用户申请,而当你读完之后,你需要写的权限,那么你又要向用户申请,这样你可以为它们设置权限群,当你申明读权限的同时,申请写的权限,这样之后你就不用再次申请写的权限了。

更多相关文章

  1. Android(安卓)Launcher一些资源
  2. Android(安卓)中从res/values/strings.xml中读取具体的字符串的
  3. Android工具箱之Activity生命周期
  4. Android(安卓)Studio 解决Fetching android sdk component infor
  5. Android工具箱之迁移到AppCompat
  6. Android(安卓)中从res/values/strings.xml中读取具体的字符串的
  7. Android权限配置
  8. 浅析Android手机卫士之手机实现短信指令获取位置
  9. Android(安卓)跑马灯效果auto scroll textview简化设置和解决焦

随机推荐

  1. 增加系统最大音量
  2. 浅谈Android引用计数(2)
  3. android在fc8上内核编译笔记
  4. listview更改选中时item背景色
  5. 关于android 多媒体数据库
  6. 使用android中自己实现可用xml配置的动画
  7. ListView背景
  8. 从别的包发起另一个包的活动以及RadioBut
  9. 写个android下倒计时软件
  10. android之startActivityForResult的使用