Android M新特性之Permissions
User does not have to grant any permissions when they install or upgrade the app. Instead, the app requests permissions as it needs them, and the system shows a dialog to the user asking for the permission.
Overview
With the M Developer Preview, the platform introduces a new app permissions model. Here's a summary of the key components of this new model:
- Declaring Permissions: The app declares all the permissions it needs in the manifest, as in earlier Android platforms.
- Permission Groups: Permissions are divided into permission groups, based on their functionality. For example, the
CONTACTS
permission group contains permissions to read and write the user's contacts and profile information. Limited Permissions Granted at Install Time: When the user installs or updates the app, the system grants the app all permissions listed in the manifest that fall under
PROTECTION_NORMAL
. For example, alarm clock and internet permissions fall underPROTECTION_NORMAL
, so they are automatically granted at install time. For more information about how normal permissions are handled, see Normal Permissions.The system may also grant the app signature permissions, as described in System components and signature permissions. The user is not prompted to grant any permissions at install time.
- User Grants Permissions at Run-Time: When the app requests a permission, the system shows a dialog to the user, then calls the app's callback function to notify it whether the user granted the permission.
This permission model changes the way your app behaves for features that require permissions. Here's a summary of the development practices you should follow to adjust to this model:
- Always Check for Permissions: When the app needs to perform any action that requires a permission, it should first check whether it has that permission already. If it does not, it requests to be granted that permission. You do not need to check for permissions that fall under
PROTECTION_NORMAL
. - Handle Lack of Permissions Gracefully: If the app is not granted an appropriate permission, it should handle the failure cleanly. For example, if the permission is just needed for an added feature, the app can disable that feature. If the permission is essential for the app to function, the app might disable all its functionality and inform the user that they need to grant that permission.
Figure 1. Permission screen in the app's Settings.
- Permissions are Revocable: Users can revoke an app's permissions at any time. If a user turns off an app's permissions, the app is notnotified. Once again, your app should verify that it has needed permissions before performing any restricted actions.
Note: If an app targets the M Developer Preview, it must use the new permissions model.
As of the launch of the M Developer Preview, not all Google apps fully implement the new permissions model. Google is updating these apps over the course of the M Developer Preview to properly respect Permissions toggle settings.
Note: If your app has its own API surface, do not proxy permissions without first ensuring the caller has the requisite permissions to access that data.
Permission groups
Related permissions are divided into permission groups to allow users to grant related permissions to an app in a single action. The user only has to grant permission once per app for each permission group. If the app subsequently requests a permission from the same permission group, the system automatically grants the permission without any action from the user. The system calls your app's onRequestPermissionsResult()
method just as if the user had granted permission through the dialog box.
For example, suppose an app lists in its manifest that it needs the SEND_SMS
and RECEIVE_SMS
permissions, which both belong to android.permission-group.SMS
. When the app needs to send a message, it requests theSEND_SMS
permission. The system shows the user a dialog box asking if the app can have access to SMS. If the user agrees, the system grants the app the SEND_SMS
permission it requested. Later, the app requestsRECEIVE_SMS
. The system automatically grants this permission, since the user had already approved a permission in the same permission group.
System components and signature permissions
Ordinarily, when the user installs an app, the system only grants the app the permissions listed in the manifest that fall under PROTECTION_NORMAL
. However, under some circumstances the system grants the app more permissions:
- System components automatically receive all the permissions listed in their manifests.
- If the app requests permissions in the manifest that fall under
PROTECTION_SIGNATURE
, and the app is signed with the same certificate as the app that declared those permissions, the system grants the requesting app those permissions on installation. Apps cannot request signature permissions at runtime.
Forwards and backwards compatibility
If an app does not target the M Developer Preview, the app continues to use the old permissions model even on M Preview devices. When the user installs the app, the system asks the user to grant all permissions listed in the app's manifest.
Note: On devices running the M Developer Preview, a user can turn off permissions for any app (including legacy apps) from the app's Settings screen. If a user turns off permissions for a legacy app, the system silently disables the appropriate functionality. When the app attempts to perform an operation that requires that permission, the operation will not necessarily cause an exception. Instead, it might return an empty data set, signal an error, or otherwise exhibit unexpected behavior. For example, if you query a calendar without permission, the method returns an empty data set.
If you install an app using the new permissions model on a device that is not running the M Preview, the system treats it the same as any other app: the system asks the user to grant all declared permissions at install time.
Note: For the preview release, you must set the minimum SDK version to the M Preview SDK to compile with the preview SDK. This means you will not be able to test such apps on older platforms during the developer preview.
Permissions versus intents
In many cases, you can choose between two ways for your app to perform a task. You can have your app ask for permission to perform the operation itself. Alternatively, you can have the app use an intent to have another app perform the task.
For example, suppose your app needs to be able to take pictures with the device camera. Your app can request the android.permission.CAMERA
permission, which allows your app to access the camera directly. Your app would then use the camera APIs to control the camera and take a picture. This approach gives your app full control over the photography process, and lets you incorporate the camera UI into your app.
However, if you don't need such control, you can just use an ACTION_IMAGE_CAPTURE
intent to request an image. When you start the intent, the user is prompted to choose a camera app (if there isn't already a default camera app), and that app takes the picture. The camera app returns the picture to your app's onActivityResult()
method.
Similarly, if you need to make a phone call, access the user's contacts, and so on, you can do that by creating an appropriate intent, or you can request the permission and access the appropriate objects directly. There are advantages and disadvantages to each approach.
If you use permissions:
- Your app has full control over the user experience when you perform the operation. However, such broad control adds to the complexity of your task, since you need to design an appropriate UI.
- The user is prompted to give permission once, the first time you perform the operation. After that, your app can perform the operation without requiring additional interaction from the user. However, if the user doesn't grant the permission (or revokes it later on), your app becomes unable to perform the operation at all.
If you use an intent:
- You do not have to design the UI for the operation. The app that handles the intent provides the UI. However, this means you have no control over the user experience. The user could be interacting with an app you've never seen.
- If the user does not have a default app for the operation, the system prompts the user to choose an app. If the user does not designate a default handler, they may have to go through an extra dialog every time they perform the operation.
Coding for Runtime Permissions
If your app targets the new M Developer Preview, you must use the new permissions model. This means that in addition to declaring your needed permissions in the manifest, you must also check to see if you have the permissions at run time, and request the permissions if you do not already have them.
Enabling the new permissions model
To enable the new M Developer Preview permissions model, set the app's targetSdkVersion
attribute to "MNC"
, and compileSdkVersion
to "android-MNC"
. Doing so enables all the new permissions features.
For the preview release, you must set minSdkVersion
to "MNC"
to compile with the preview SDK.
Designating a permission for the M Preview only
You can use the new <uses-permission-sdk-m>
element in the app manifest to indicate that a permission is only needed on the M Developer Preview. If you declare a permission this way, then whenever the app is installed on an older device, the system does not prompt the user or grant the permission to the app. By using the <uses-permission-sdk-m>
element, you can add new permissions to updated versions of your app without forcing users to grant permissions when they install the update.
If the app is running on a device with the M Developer Preview, <uses-permission-sdk-m>
behaves the same as<uses-permission>
. The system does not prompt the user to grant any permissions when they install the app, and the app requests permissions as they are needed.
Prompting for permissions
If your app uses the new M Developer Preview permissions model, the user is not asked to grant all permissions when the app is first launched on a device running the M Preview. Instead, your app requests permissions as they are needed. When your app requests a permission, the system shows a dialog to the user.
If your app runs on a device that has SDK 22 or lower, the app uses the old permissions model. When the user installs the app, they are prompted to grant all the permissions your app requests in its manifest, except for those permissions which are labeled with <uses-permission-sdk-m>
.
Check what platform the app is running on
This permissions model is only supported on devices running the M Developer Preview. Before calling any of these methods, the app should verify what platform it's running on by checking the value of Build.VERSION.CODENAME
. If the device is running the M Developer Preview, CODENAME
is "MNC"
.
Check if the app has the needed permission
When the user tries to do something that requires a permission, the app checks to see if it currently has permission to perform this operation. To do this, the app calls Context.checkSelfPermission(permission_name)
. The app should perform this check even if it knows the user has already granted that permission, since the user can revoke an app's permissions at any time. For example, if a user wants to use an app to take a picture, the app callsContext.checkSelfPermission(Manifest.permission.CAMERA)
.
Android M新特性之Permissions的更多相关文章
- 可能是最早的学习Android N新特性的文章
可能是最早的学习Android N新特性的文章 Google在今天放出了Android N开发者预览版.Android N支持Nexus6及以上的设备.5太子Nexus5不再得到更新. Android ...
- Android O新特性和行为变更总结zz
https://mp.weixin.qq.com/s/Ezfm-Xaz3fzsaSm0TU5LMw Android O 行为变更https://developer.android.google.cn/ ...
- Android N 新特性
2016年5月19日,谷歌在美国加州的山景城举办了 Google I/O 开发者大会中发布.2016年6月,Android N正式命名为“牛轧糖” 本届I/O开发者大会上,Google重点介绍了And ...
- Android R 新特性分析及适配指南
Android R(Android 11 API 30)于2020年9月9日正式发布,随国内各终端厂商在售Android设备的版本更新升级,应用软件对Android R 版本的兼容适配已迫在眉睫. 对 ...
- 从开发者角度解析 Android N 新特性!
大清早看到 Google 官方博客发布 Android N 的开发者预览版,立马从床上跳起来开始仔仔细细的读起来. 从开发者角度来看,Android N 的更新并不算大.网上之前流传的一些 Andro ...
- Android 13 新特性及适配指南
Android 13(API 33)于 2022年8月15日 正式发布(发布时间较往年早了一些),正式版Release源代码也于当日被推送到AOSP Android开源项目. 截止到笔者撰写这篇文章时 ...
- Android N 新特性 + APP开发注意事项
1. 多窗口MultiWindow 多窗口MultiWindow,这是Android N里对开发者影响比较大的特性,也是大家疑问比较多的地方.站在开发者的角度其实不必太担心这个特性会导致我们需要修改很 ...
- Android开发学习之路-Android N新特性-多窗口模式
我们都知道,在最新的Android N系统中,加入了一个新的功能,就是多窗口模式.多窗口模式允许我们在屏幕上显示两个窗口,每个窗口显示的内容不同,也就是说,我们可以一遍看电视剧,一边聊微信. 这里我们 ...
- Android M新特性之Behavior Changes
1.Runtime Permissions On your apps that target the M Preview release or higher, make sure to check f ...
随机推荐
- checkbox的单选全选,反选,计算价格,删除
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- 161213、Maven资源替换和Freemarker模板
先介绍一下本文的两位主角: Apache Maven - 正当红的项目管理工具 FreeMarker - 老牌的模板引擎 两者貌似互不相干,何来冲突呢? 原来Maven有一个内置的资源替换机制, 可以 ...
- .getClass();
Java的每个类都带有一个运行时类对象,该Class对象中保存了创建对象所需的所有信息.可以用.class返回此 Object 的运行时类Class对象,也可以用getClass()获得.获得此对象后 ...
- 关于android接口回调机制
http://www.cnblogs.com/JohnTsai/p/3975022.html http://www.zhihu.com/question/19801131 In my previous ...
- 【译】微型ORM:PetaPoco【不完整的翻译】
PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...
- 为什么要在html和body加上“height:100%;”
元素中有内容的时候div才能被撑起来所以我给div加了背景但是也不显示,就是因为没有内容,这个时候的解决办法就是 html,body{ height:100%; }
- bootstrap-table 加载不了数据问题总结
1.Without server-side pagination data-side-pagination="client"(bs-table的设置) 服务器端代码: @Reque ...
- R----Shiny包介绍学习
为什么用Shiny Shiny让数据分析师写完分析与可视化代码后,稍微再花几十分钟,就可以把分析代码工程化,将分析成果快速转化为交互式网页分享给别人.所以,如果你是一名使用R的数据分析师,选择Shin ...
- 使用VB6制作RTD函数
以前模仿大神在vs里使用c#实现RTD函数功能.(真是很生僻的东东啊)C#制作RTD参考:大神博客跳转.最近想VB里能不能做?就试着做了做,好像基本成了,整套代码有些毛病,勉强能算个样子,暂时不打算再 ...
- Page Visibility API(页面可见性)
页面可见性: 就是对于用户来说,页面是显示还是隐藏, 所谓显示的页面,就是我们正在看的页面:隐藏的页面,就是我们没有看的页面. 因为,我们一次可以打开好多标签页面来回切换着,始终只有一个页面在我们眼前 ...