AndPermission
This open source library is called AndPermission: https://github.com/yanzhenjie/AndPermission After my practice, I have completely solved the above problems, recommend you to use, interested friends can go to star.
AndroidStudio usage, gradle one sentence remote dependency
compile 'com.yanzhenjie:permission:1.0.8'
Or Maven:
<dependency>
<groupId>com.yanzhenjie</groupId>
<artifactId>permission</artifactId>
<version>1.0.5</version>
<type>pom</type>
</dependency>
Eclipse downloads the jar package or the source code.
Apply for permission
A privilege
AndPermission.with(this)
.requestCode(100)
.permission(Manifest.permission.WRITE_CONTACTS)
.send();
Multiple Permissions
AndPermission.with(this)
.requestCode(100)
.permission(Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_SMS)
.send();
When using special permissions, you only need to call them directly in Activity, Fragment, and then execute the corresponding code when AndPermission calls back.
Be careful
1. If your Activity inherits AppCompatActivity, FragmentActivity, or their subclasses, you can request permissions directly.
2. If your Fragment inherits android.support.v4.app.Fragment or its subclasses, you can request permissions directly.
3. If you inherit from android.app.Activity, android.app.Fragment, and your phone is under 6.0 without onRequestPermissionsResult() method, you need to make a judgment before applying for permission:
//First determine if you have permission.
if(AndPermission.hasPermission(this, Manifest.permission.READ_SMS)) {
// Have permission, do anything directly.
} else {
// Apply for permission.
AndPermission.with(this)
.requestCode(100)
.permission(Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_SMS)
.send();
}
Callback result
Mode 1: Callback using Listener
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// All you need to do is call this sentence, let's pass the rest to AndPermission, and the last parameter is PermissionListener.
AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, listener);
}
private PermissionListener listener = new PermissionListener() {
@Override
public void onSucceed(int requestCode, List<String> grantedPermissions) {
// Permission request successfully callback.
if(requeust == 100) {
// TODO corresponding code.
} else if(requestCode == 101) {
// TODO corresponding code.
}
}
@Override
public void onFailed(int requestCode, List<String> deniedPermissions) {
// Permission application failed callback.
// If the user checks not to prompt again and refuses permission, prompt the user to authorize in the settings.
if (AndPermission.hasAlwaysDeniedPermission(this, deniedPermissions)) {
// First: Use the default prompt.
AndPermission.defaultSettingDialog(this, REQUEST_CODE_SETTING).show();
// Second: Use a custom prompt.
// AndPermission.defaultSettingDialog(this, REQUEST_CODE_SETTING)
// .setTitle("Permission application failed")
// .setMessage("Some of the permissions we need have been rejected by you or the system failed to apply for errors. Please go to the settings page and authorize manually, otherwise the function will not work properly!")
// .setPositiveButton("OK, go set")
// .show();
// Third: Customize the dialog style.
// SettingService settingService =
// AndPermission.defineSettingDialog(this, REQUEST_CODE_SETTING);
// Your dialog clicked OK to call:
// settingService.execute();
// Your dialog clicked Cancel:
// settingService.cancel();
}
}
};
Mode 2: Callback using annotations
You just need to override a method of Activity/Fragment and provide a method to call back when authorizing:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// Just call this sentence, the first parameter is the current Acitivity/Fragment, and the callback method is written in the current Activity/Framgent.
AndPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
}
// The successful callback method is annotated with the number requestCode at the time of the request.
@PermissionYes(100)
private void getLocationYes(List<String> grantedPermissions) {
// TODO successfully applied for permissions.
}
// The method of a failed callback is simply a comment with the number requestCode at the time of the request.
@PermissionNo(100)
private void getLocationNo(List<String> deniedPermissions) {
// If the user checks not to prompt again and refuses permission, prompt the user to authorize in the settings.
if (AndPermission.hasAlwaysDeniedPermission(this, deniedPermissions)) {
// First: Use the default prompt.
AndPermission.defaultSettingDialog(this, REQUEST_CODE_SETTING).show();
}
}
If you use it, you can do it boldly, and AndPermission does it automatically for all the complex logic that is described in the blog.
After Rationale rejects once, prompt the user again for the role of permissions
Mode 1: Use the AndPermssion default MD Style dialog
AndPermission.with(this)
.requestCode(REQUEST_CODE_PERMISSION_LOCATION)
.permission(Manifest.permission.ACCESS_FINE_LOCATION)
.rationale((requestCode, rationale) ->
// This dialog can be customized to call rational.resume()You can continue your application.
AndPermission.rationaleDialog(PermissionActivity.this, rationale).show()
)
.send()
Mode 2: Customize the dialog box
AndPermission.with(this)
.requestCode(REQUEST_CODE_PERMISSION_LOCATION)
.permission(Manifest.permission.ACCESS_FINE_LOCATION)
.rationale(rationaleListener)
.send()
/**
* Rationale Support, customize the dialog here.
*/
private RationaleListener rationaleListener = (requestCode, rationale) -> {
AlertDialog.build(this)
.setTitle("Friendly reminders")
.setMessage("You have refused the right to locate. Without the right to locate, you can't recommend nearby girls. Please give me the right to locate!")
.setPositiveButton("OK, here you are", (dialog, which) -> {
rationale.resume();
})
.setNegativeButton("I decline", (dialog, which) -> {
rationale.cancel();
}).show();
};
confusion
If a Listener is used to accept callback results, no configuration is required.
Callback the results using annotations, adding the following configuration to proguard-rules.pro
-keepclassmembers class ** {
@com.yanzhenjie.permission.PermissionYes <methods>;
}
-keepclassmembers class ** {
@com.yanzhenjie.permission.PermissionNo <methods>;
}