The original article, please indicate (reprinted from: blog.csdn.net/leejizhou/a… Lee Jeju’s blog)
Quote: Requesting Permissions at Run Time has attracted the attention of developers since the release of Android 6.0 last year. With the popularity of the 6.0 system by domestic mobile phone manufacturers this year, I think it is necessary for everyone to understand this new feature. Because developing in TargetSDK23+ without paying attention to this will cause the APP to crash on 6.0+ phones, this post will explore this new feature.
In previous SDK development, if you need to use some permissions such as call or text, you can just configure it in AndroidManifest, However, SDK23+ uses Dangerous Permissions, which is not only configured in AndroidManifest, but requires the user to grant Permissions before the operation takes place, much like iOS Permissions. If the user does not grant permission or refuses permission to perform operations such as making a phone call, the software will crash.
* if your app TargetSDK is set below 23, it will not crash in 6.0+. Of course, you can always set TargetSDK below 23, so you don’t need to read this article 🙂
So which permissions need to be granted at run time? Take a look at the official statement
Dangerous permissions
These permissions need to be configured not only in AndroidManifest, but also at runtime for the user to grant permissions to use these features.
As you can see, making a call requires runtime authorization, so let’s do a little click-on-button call demo to see how this runtime authorization works.
1: the targetSdkVersion of the APP needs to be set above 23 and run on Android6.0 or above
2: Configure the rights to make calls in AndroidManifest
Copy the code
3: We simply put a button in the layout and click to make a call (omitted)
4.1: What happens if you don’t do runtime permission processing? We go straight to the button to make the call
public void CallPhone(View v) {
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + "10010");
intent.setData(data);
startActivity(intent);
}Copy the code
Operation effect:
You can see that running on Android 6.0 will crash
4.2: We add runtime permission processing before running
private static final int REQUESTCODE = 8;
public void CallPhone(View v) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CALL_PHONE},
REQUESTCODE);
} else {
call();
}
}Copy the code
API: REQUESTCODE is used for callback processing, since requesting permissions has callback results that will be explained later.
ContextCompat. Detection of checkSelfPermission is mainly used for certain whether permission has been granted, Method parameters (the context, the need to detect permissions) method return values for the PackageManager. PERMISSION_DENIED or PackageManager. PERMISSION_GRANTED, To be called when the return PackageManager. PERMISSION_DENIED API for permission to apply for.
ActivityCompat requestPermissions used to access application, the method parameters for (the context, the need to apply for permission to array, the request of the custom code), the system will pop up a dialog application permissions.
Operation effect:
You can see that the program has successfully placed the call.
4.3 But what if the user refuses this permission and sets no reminding? Click the button and there will be no response, which is very unfriendly, as follows.
Change the code a little bit
public void CallPhone(View v) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) ! = PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {new alertdialog.builder (mainactivity.this).setMessage(" App requires permission to use this function ") SetPositiveButton (" set ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + getPackageName())); startActivity(intent); }}).setnegativeButton (" Cancel ", null).create().show(); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUESTCODE); } } else { call(); }}Copy the code
Here we prior to application for permission to add a judge ActivityCompat. ShouldShowRequestPermissionRationale (this, The manifest.permission.CALL_PHONE method takes (context, permission to check) as an argument. If it returns true, the user selected reject last time they clicked on it, so we do some friendly prompts.
Operation effect:
5: OK Finally, the callback method of permission application is introduced
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case REQUESTCODE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { call(); } else { } return; }}}Copy the code
The grantResults array represents the result array of permissions. The grantResults array represents the result array of permissions. The grantResults array represents the result array of permissions. So onRequestPermissionsResult return results in an array, the array grantResults [n]. = = PackageManager PERMISSION_GRANTED represents the permissions have been authorized by the user.
Finally, let’s look at the complete code
/** * blog:www.lijizhou.com */ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } private static final int REQUESTCODE = 8; public void CallPhone(View v) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) ! = PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {new alertdialog.builder (mainactivity.this).setMessage(" App requires permission to use this function ") SetPositiveButton (" set ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + getPackageName())); startActivity(intent); }}).setnegativeButton (" Cancel ", null).create().show(); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUESTCODE); } } else { call(); } } private void call() { Intent intent = new Intent(Intent.ACTION_CALL); Uri data = Uri.parse("tel:" + "10010"); intent.setData(data); startActivity(intent); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case REQUESTCODE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { call(); } else { } return; }}}}Copy the code
OK, a runtime permissions using android original API finished processing is introduced, of course, you also can be used to encapsulate convenient, welcome to leave a message below, this source download.csdn.net/detail/leej download address…