Overview:
To protect system integrity and user privacy, Android runs each app in a restricted sandbox.
If your app needs to use resources or information other than its sandbox, you must explicitly request permissions.
Depending on the type of permission requested by the app, the system may automatically grant permissions, or users may be required to grant permissions.
Permissions Best Practices:
1. Consider using the intent example: [email protected] This.startactivityforresult (takeintent, 1)--this priority, easy to use
2. If you use permissions
- Only ask for the permissions you need, don't ask too much;
- Apply for permission, not a one-time application, where needed;
- Explain why the need for permission is not lengthy;
- A corresponding prompt is required to deny permissions.
Pros and Cons: If you use permissions:
Your app can take full control of the user experience when you perform actions. However, such extensive control increases the complexity of the task because you need to design the appropriate UI.
The user is prompted to give permission once the app is running or installed (depending on the user's Android version). The app can then perform actions that no longer require users to interact with others. However, if the user does not grant permissions (or revoke permissions later), your app will not be able to perform the operation at all.
If you use intent:
You do not need to design the UI for the operation. Apps that handle intent will provide the UI. However, this means that you cannot control the user experience. Users may interact with apps that you've never seen before.
If the user does not have a default app for the action, the user is prompted to select an app. If the user does not specify a default handler, they must process an additional dialog box each time they perform this action.
Usage Rights Practices:
1. Declare the required permissions in the app manifest.
1.1 In all versions of Android, your app needs to declare both the normal and dangerous permissions it needs in its app manifest.
2. The runtime requests the user to grant permissions. Applies only to devices running Android 6.0 (API level 23) and later.
2.1 The behavior of the system after you declare permissions depends on the sensitivity of the permissions. If permissions do not affect user privacy, the system is automatically authorized.
If the permission may involve access to sensitive user information, the user is asked to approve the request.
To learn more about the different kinds of permissions, see Normal permissions and Dangerous permissions.
2.2 Use the Android support library to check and request permissions. The Android Framework provides a similar approach starting with Android 6.0 (API level 23).
However, it's easier to use a support library because your app doesn't need to check which version of Android it's running on before calling the method.
(AppCompat library, typically, the name of ... This is true for classes that end Compat (such as Activitycompat). )
2.3 To check if you have a permission, call the Contextcompat.checkselfpermission () method.
For example, the following code snippet shows how to check if Activity has permission to write in the calendar:
Assume thisactivity is the current activity
int permissioncheck = contextcompat.checkselfpermission (Thisactivity,manifest.permission.write_calendar);
If the app has this permission, the method returns packagemanager.permission_granted, and the app can continue the operation. If the app does not have this permission, the method returns Permission_denied, and the app must explicitly ask the user for permissions.
PackagePers.hbolin.permissionsdemoImportAndroid. ManifestImportandroid.app.ActivityImportAndroid.content.pm.PackageManagerImportandroid.support.v7.app.AppCompatActivityImportAndroid.os.BundleImportAndroid.support.v4.app.ActivityCompatImportAndroid.support.v4.content.ContextCompatImportAndroid.support.v7.app.AlertDialogImportAndroid.util.LogImportkotlinx.android.synthetic.main.activity_main.*ImportAndroid.provider.MediaStoreImportandroid.content.IntentImportAndroid.graphics.Bitmapclassmainactivity:appcompatactivity () {Val TAG= "Mainactivity"Override Fun OnCreate (Savedinstancestate:bundle?) { Super. OnCreate (savedinstancestate) Setcontentview (R.layout.activity_main) callreadcontacts () button.se Tonclicklistener {//with intent, you do not have to configure permissions in Androidmanifest.xml//And you don't need to configure permissions dynamically, you can call//There is a lack of judgment, there is something in response to this intent, otherwise it will throw an exception. Val takeintent =Intent (mediastore.action_image_capture) [email protected] This. Startactivityforresult (Takeintent, 1) } } //Request Permission PrivateFun callreadcontacts () {//Here , thisactivity are the current activity if(Contextcompat.checkselfpermission ( This, Manifest.permission.READ_CONTACTS)! =packagemanager.permission_granted) {LOG.D (TAG,"No Permissions acquired") //should we show an explanation? if(Activitycompat.shouldshowrequestpermissionrationale ( This, Manifest.permission.READ_CONTACTS)) { //Show an expanation to the user *asynchronously*--don ' t block//This thread waiting for the user ' s response! after the user//sees the explanation, try again to request the permission.log.d (TAG,"Explaining why permissions are required") Val Builder= Alertdialog.builder ( This) Builder.settitle ("Explaining why permissions are required"). Setmessage ("Because it's in the test."). Setpositivebutton ("OK") {_, _,//Request PermissionActivitycompat.requestpermissions ( This, arrayof (Manifest.permission.READ_CONTACTS), 1)}. Setnegativebutton ("Cancel") {_, _,}. Create (). Show ()}Else { //No explanation needed, we can request the permission.Activitycompat.requestpermissions ( This, arrayof (Manifest.permission.READ_CONTACTS), 1) //My_permissions_request_read_contacts is an//app-defined INT constant. The callback method gets the//result of the request. } } Else{log.d (TAG,"Acquired Permissions") } } //Request CallbackOverride Fun Onrequestpermissionsresult (Requestcode:int, Permissions:array<out string>, Grantresults:intarray) { if(Requestcode = = 1) { if(Grantresults.isnotempty () && grantresults[0] = =packagemanager.permission_granted) {LOG.D (TAG,"Permission Consent") } Else{log.d (TAG,"Permission Forbidden") } } } //Startactivityforresult CallbackOverride Fun Onactivityresult (Requestcode:int, Resultcode:int, data:intent?)) {log.d (TAG,"Request $requestCode and result $resultCode and data is null: ${data = = null}") if(Requestcode = = 1 && ResultCode = =ACTIVITY.RESULT_OK) { if(data?. Hasextra ("data") = =true) {log.i (TAG,"Data is not NULL") Val Bitmap= data.getparcelableextra<bitmap> ("Data") log.d (TAG,"Bitmap.width is ${bitmap.width}") Imageview.setimagebitmap (bitmap)//ImageView is the control that the current page needs to display the photo, which can be replaced } } }}
Android Permissions Processing