Andrews Self Programming The Definitive Guide (xxvii)

A. Obtain permission to run

Permissions need to do three things to deal with run-time, (1) to ensure that there is not a privilege (2) to obtain permission to use, if not, then (3) listening permission request feedback

(1) First, we get the right information, open class fragments that we need to add a constant array that lists all the permissions the application requires

private static final String[] LOCATION_PERMISSIONS = new String[]{

Manifest.permission.ACCESS_FIME_LOCATION,

Manifest.permission.ACCESS_COARSE_LOCATION,

};

The above rights and permissions in AndroidManifest which is the same, the next step is to request these permissions

For the type of security permissions, the permission is given to the group, rather than individual rights, rights group that contains the specific types of rights, privileges granted to a single set of permissions inside, all other rights are authorized within the same group, so when two rights belong to the same group permission, we simply permission to apply for one on it

(2) Write a method, confirmation is not able to obtain permission first array inside LOCATION_PERMISSIONS

private boolean hasLocationPermission(){

int result = ContextComp.checkSelfPermission(getActivity(),LOCATION_PERMISSIONS[0]);

return result == PackageManager.PERMISSION_GRANTED;

}

Then we need before findImage () method to call the above method

if(hasLocationPermission()) {

findimage ();

}

(3) If the permission check is not passed, then the call requestPermission () method to request authorization

// add an authorization code

private static final int REQUEST_PERMISSIONS = 0;

if(hasLocationPermission()) {

findimage ();

}else{

requestPermissions(LOCATION_PERMISSIONS,REQUEST_PERMISSIONS);

}

requestPermissions () is an asynchronous request method after calling it, Android system will pop up dialog box to request permission to authorize user feedback, in response to user operations, we also need to write a onRequestPermissionsResult () response method, when the user clicks ALLOW or DENY button, Android will call this callback method, the results of the authorization check again, if the user is given the authorization, then call findImage () method

public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults) {

switch(requestCode) {

case REQUEST_LOCATION_PERMISSIONS:

if(hasLocationPermission()) {

findImage ();

}

default:

super.onRequestPermissionsResult(requestCode,permissions,grantResults);

}

}

For the above parameters grantResults, if you wish you can also see the value of this parameter to confirm the authorization result, but from the above code, we can see that we have adopted a more convenient way to call hasLocationPermission () method, which inside checkSelfPermission () method will give authorization result, so once again call gasLocationPermission () method can be

If we press a button to give ALLOW authorized, unless permission to unload or shut down, otherwise the application will always have the authorization, if he does, then only temporarily rejected, the next time again, or jump out of the authorization dialog

 

 

Guess you like

Origin blog.csdn.net/weixin_41673515/article/details/90415114