1*d353a188SXin Li 2*d353a188SXin LiAndroid RuntimePermissions Sample (Kotlin) 3*d353a188SXin Li========================================== 4*d353a188SXin Li 5*d353a188SXin LiThis sample shows runtime permissions available in Android M and above. 6*d353a188SXin LiIt shows how to check and request permissions at runtime, handle backwards compatibility using the 7*d353a188SXin Lisupport library and how to declare optional permissions for M-devices only. 8*d353a188SXin Li 9*d353a188SXin LiIntroduction 10*d353a188SXin Li------------ 11*d353a188SXin Li 12*d353a188SXin LiAndroid M introduced runtime permissions. Applications targeting M and above need to request their 13*d353a188SXin Lipermissions at runtime. 14*d353a188SXin Li 15*d353a188SXin LiAll permissions still need to be declared in the AndroidManifest. However, when accessing APIs that 16*d353a188SXin Lirequire a permission, the Activity or Fragment has to verify that the permission has been granted 17*d353a188SXin Lior request the missing permissions using calls through the support library. 18*d353a188SXin Li 19*d353a188SXin LiPermissions are checked through `ActivityCompat#checkSelfPermission(Context, String)` or 20*d353a188SXin LiContextCompat#checkSelfPermission(Context, String). 21*d353a188SXin Li 22*d353a188SXin LiPermission are requested through `ActivityCompat#requestPermissions(Activity, String[], int)`, and 23*d353a188SXin Lithe response received in a callback to 24*d353a188SXin Li`ActivityCompat.OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[])`. 25*d353a188SXin Li 26*d353a188SXin LiApplications can provide an additional rational for the use of permissions after calling 27*d353a188SXin Li`ActivityCompat#shouldShowRequestPermissionRationale(Activity,String)`. This call will return true 28*d353a188SXin Liif the application should provide the user with more context on why the requested permissions is 29*d353a188SXin Lineeded, for example if the permission request has been denied before. 30*d353a188SXin Li 31*d353a188SXin LiIf an application targets an SDK below M, all permissions are granted at runtime and are available 32*d353a188SXin Liwhen the application is running. The support library calls handle these checks appropriately. 33*d353a188SXin LiHowever, if permissions have been turned off in the system settings 34*d353a188SXin Lifor an application targeting an SDK below M, the API will return empty or no data. 35*d353a188SXin Li 36*d353a188SXin LiPre-requisites 37*d353a188SXin Li-------------- 38*d353a188SXin Li 39*d353a188SXin Li- Android SDK 26 40*d353a188SXin Li- Android Support Repository 41*d353a188SXin Li 42*d353a188SXin LiScreenshots 43*d353a188SXin Li------------- 44*d353a188SXin Li 45*d353a188SXin Li<img src="screenshots/screenshot-1.png" height="400" alt="Screenshot"/> 46*d353a188SXin Li<img src="screenshots/screenshot-2.png" height="400" alt="Screenshot"/> 47*d353a188SXin Li 48*d353a188SXin LiGetting Started 49*d353a188SXin Li--------------- 50*d353a188SXin Li 51*d353a188SXin LiThis sample uses the Gradle build system. To build this project, use the 52*d353a188SXin Li"gradlew build" command or use "Import Project" in Android Studio. 53*d353a188SXin Li 54*d353a188SXin LiSupport 55*d353a188SXin Li------- 56*d353a188SXin Li 57*d353a188SXin Li- Google+ Community: https://plus.google.com/communities/105153134372062985968 58*d353a188SXin Li- Stack Overflow: http://stackoverflow.com/questions/tagged/android 59*d353a188SXin Li 60*d353a188SXin LiIf you've found an error in this sample, please file an issue: 61*d353a188SXin Lihttps://github.com/googlesamples/android-RuntimePermissions 62*d353a188SXin Li 63*d353a188SXin LiPatches are encouraged, and may be submitted by forking this project and 64*d353a188SXin Lisubmitting a pull request through GitHub. Please see CONTRIBUTING.md for more details. 65*d353a188SXin Li 66*d353a188SXin LiLicense 67*d353a188SXin Li------- 68*d353a188SXin Li 69*d353a188SXin LiCopyright 2017 The Android Open Source Project, Inc. 70*d353a188SXin Li 71*d353a188SXin LiLicensed to the Apache Software Foundation (ASF) under one or more contributor 72*d353a188SXin Lilicense agreements. See the NOTICE file distributed with this work for 73*d353a188SXin Liadditional information regarding copyright ownership. The ASF licenses this 74*d353a188SXin Lifile to you under the Apache License, Version 2.0 (the "License"); you may not 75*d353a188SXin Liuse this file except in compliance with the License. You may obtain a copy of 76*d353a188SXin Lithe License at 77*d353a188SXin Li 78*d353a188SXin Lihttp://www.apache.org/licenses/LICENSE-2.0 79*d353a188SXin Li 80*d353a188SXin LiUnless required by applicable law or agreed to in writing, software 81*d353a188SXin Lidistributed under the License is distributed on an "AS IS" BASIS, WITHOUT 82*d353a188SXin LiWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 83*d353a188SXin LiLicense for the specific language governing permissions and limitations under 84*d353a188SXin Lithe License. 85