1<?xml version="1.0" encoding="UTF-8"?> 2<!-- 3 Copyright 2015 The Android Open Source Project 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16--> 17<sample> 18 <name>RuntimePermissions</name> 19 <group>System</group> 20 <package>com.example.android.system.runtimepermissions</package> 21 22 <minSdk>15</minSdk> 23 24 <dependency>com.android.support:appcompat-v7:23.0.0</dependency> 25 <dependency>com.android.support:support-v4:23.0.0</dependency> 26 <dependency>com.android.support:design:23.0.0</dependency> 27 28 <strings> 29 <intro> 30 <![CDATA[ 31 This sample shows runtime permissions available in Android M and above. 32 Display the log on screen to follow the execution. 33 If executed on an Android M device, an additional option to access contacts is shown 34 that is declared with optional, M and above only permissions. 35 ]]> 36 </intro> 37 </strings> 38 39 <template src="base"/> 40 <common src="logger"/> 41 42 <metadata> 43 <status>DRAFT</status> 44 <categories>System</categories> 45 <technologies>Android</technologies> 46 <languages>Java</languages> 47 <solutions>Mobile</solutions> 48 <level>BEGINNER</level> 49 <icon>screenshots/big_icon.png</icon> 50 <screenshots> 51 <img>screenshots/screenshot-1.png</img> 52 <img>screenshots/screenshot-2.png</img> 53 </screenshots> 54 <api_refs> 55 <android>android.app.Activity</android> 56 <android>android.support.v4.app.ActivityCompat</android> 57 <android>android.Manifest.permission</android> 58 </api_refs> 59 60 <description> 61<![CDATA[ 62This sample shows runtime permissions available in Android M and above. 63It shows how to check and request permissions at runtime, handle backwards compatibility using the 64support library and how to declare optional permissions for M-devices only. 65]]> 66 </description> 67 68 <intro> 69<![CDATA[ 70Android M introduced runtime permissions. Applications targeting M and above need to request their 71permissions at runtime. 72All permissions still need to be declared in the AndroidManifest. However, when accessing APIs that 73require a permission, the Activity or Fragment has to verify that the permission has been granted 74or request the missing permissions using calls through the support library. Permissions are checked 75through ActivityCompat#checkSelfPermission(Context, String) or 76ContextCompat#checkSelfPermission(Context, String). 77Permission are requested through ActivityCompat#requestPermissions(Activity, String[], int), and the response 78received in a callback to ActivityCompat.OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[]). 79Applications can provide an additional rational for the use of permissions after calling 80ActivityCompat#shouldShowRequestPermissionRationale(Activity,String). This call will return true if the 81application should provide the user with more context on why the requested permissions is needed, 82for example if the permission request has been denied before. 83 84If an application targets an SDK below M, all permissions are granted at runtime and are available 85when the application is running. The support library calls handle these checks appropriately. 86However, if permissions have been turned off in the system settings 87for an application targeting an SDK below M, the API will return empty or no data. 88]]> 89 </intro> 90 </metadata> 91</sample> 92