Introduction to run-time dynamic permissions
All rights declared in Androidmanifest are granted by default in apps with targetsdkversion less than 23 without manual grant. When Targetsdkversion is 23 or more, the app does not grant the "dangerous" level permission by default, and Android defaults to all permissions for that group by granting it a permission.
Dynamic permission fetching
The following shows how to call the system camera to take pictures and record videos with run-time permission fetching
First, the debug application is compiled with Targetsdkversion 23来, to call the camera to take pictures and store the following two permissions to be declared in the Androidmanifest.xml file:
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
As for the interface code I do not put up here, can download source view. This time (does not join runtime dynamic permission detection) If you debug the app, click the Take photo button you will find that the app crashes, in fact, the reason is very simple, the application does not get the above two permissions.
Therefore, starting from Targetsdkversion 23, the "Dangerous" level of permissions we want to turn on the runtime dynamic permission detection, detect whether there is a corresponding permission code as follows:
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { requestPermission(); } else { openCamera(v); }
The Checkselfpermission function detects if a permission is present, and the function has two versions (the latter version is recommended) checkSelfPermission(String permission)
and ActivityCompat.checkSelfPermission(Context context, String permission)
. The previous version was introduced in API 23 and can only be used in API 23 and above. The latter version is in the support V4 package, and can be used to eliminate the version detection code. The return value of the function is only two PackageManager.PERMISSION_GRANTED
and the PackageManager.PERMISSION_DENIED
permission is granted and denied. Here The IF statement determines whether the permission is granted, and if the permission is missing, call Requestpermission () to request permission, otherwise proceed to the next step.
/** * Request permission, request more than one time */ Private void requestpermission() {if(Activitycompat.shouldshowrequestpermissionrationale ( This, Manifest.permission.CAMERA) | | Activitycompat.shouldshowrequestpermissionrationale ( This, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {Snackbar.make (fl,"need to get related permissions", Snackbar.length_indefinite). Setaction ("OK",NewView.onclicklistener () {@Override Public void OnClick(View view) {activitycompat.requestpermissions (mainactivity). This,NewString[]{manifest.permission.camera, Manifest.permission.WRITE_EXTERNAL_STORAGE}, Reque St_permission); }}). Setactiontextcolor (Getresources (). GetColor (R.color.coloraccent)) . Show (); }Else{activitycompat.requestpermissions (mainactivity). This,NewString[]{manifest.permission.camera, Manifest.permission.WRITE_EXTERNAL_STORAGE}, Request_permission); } }/** * Callback to request permission result * * @param requestcode * @param permissions * @param gr Antresults * * @Override Public void Onrequestpermissionsresult(intRequestcode, @NonNull string[] permissions, @NonNullint[] grantresults) {Super. Onrequestpermissionsresult (Requestcode, permissions, grantresults);if(Requestcode = = request_permission) {if(grantresults[0] = = Packagemanager.permission_denied && grantresults[1] = = packagemanager.permission_denied) {Snackbar.make (fl,"No permissions Granted", Snackbar.length_short). Show (); }Else if(grantresults[0] = = packagemanager.permission_granted && grantresults[1] = = packagemanager.permission_denied) {Snackbar.make (fl,"Read and write storage permissions not granted", Snackbar.length_short). Show (); }Else if(grantresults[0] = = Packagemanager.permission_denied && grantresults[1] = = packagemanager.permission_granted) {Snackbar.make (fl,"Open Camera permissions not granted", Snackbar.length_short). Show (); }Else if(grantresults[0] = = packagemanager.permission_granted && grantresults[1] = = packagemanager.permission_granted) {Opencamera (view); } } }
Requestpermissions is a permission request function, which is no longer discussed here, as is the two version of the checkselfpermission. The function has a callback function Onrequestpermissionsresult: Here you can tell whether a permission is granted or denied, and then proceed to the next step.
The Shouldshowrequestpermissionrationale function, like Checkselfpermission, is also a two version, which is no longer discussed here. Here is a detailed description of the return value of the Shouldshowrequestpermissionrationale function:
1, the first call always returns FALSE, so call Requestpermissions directly to request a permission group,
2, when the first request permission Click Deny, if you request permission again, then return true, this time the Permission dialog box will be a ' no longer ask ' option. If this option is checked, only the Deny permission can be selected, at which point the deny can only go to the app Management grant permission or clear the data request again.
3. If you check the no longer ask option and Deny permission, again the right time to return false, the Permission dialog box is not displayed, all permissions are denied by default, and directly callback the Onrequestpermissionsresult function.
Note: Snackbar is a new control in the Android support library, similar to toast but better than toast, for more information please see my other blog on Android Material design Snackbars
The code for the Opencamera function is as follows, which is understood at a glance and is no longer discussed here.
/** * Open Camera * * @param View * * Private void Opencamera(View view) {file =NewFile (environment.getexternalstoragedirectory () + File.separator +"CameraDemo1");if(!file.exists ()) {File.mkdirs (); } vv.setvisibility (View.gone); Iv.setvisibility (View.gone);Switch(View.getid ()) { CaseR.id.bt_picture:name =NewSimpleDateFormat ("Yyyymmdd_hhmmss"). Format (NewDate ()); URI = Uri.fromfile (NewFile (File.getpath () + file.separator + name +". jpg"));//Start System camera PhotoIntent Intent =NewIntent (mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, URI); Startactivityforresult (intent, picture); Break; CaseR.id.bt_video:name =NewSimpleDateFormat ("Yyyymmdd_hhmmss"). Format (NewDate ()); URI = Uri.fromfile (NewFile (File.getpath () + file.separator + name +". mp4"));//Start System camera recordingIntent intent1 =NewIntent (mediastore.action_video_capture); Intent1.putextra (Mediastore.extra_output, URI); Intent1.putextra (Mediastore.extra_video_quality,1); Startactivityforresult (Intent1, VIDEO); Break; } }
Androidstudio Source Download
Android Runtime dynamic permission acquisition