Copy the libray module to the project, or directly rely on the following in build.gradle:
allprojects { repositories { maven { url 'https://jitpack.io' } } }
dependencies { compile 'com.github.AnJiaoDe:Permission:V1.0.0' }
Note: if sync reports an error, it is related to com.android.tools.build:gradle 3.0,
You can change compile to implementation or api instead
For Android 6.0 and above, the danger permission must be dynamically requested, and the danger permission is divided into groups
1. All allowed
2. refusal
3. refusal
4. The user refuses and selects no more queries. The pop-up window allows the user to authorize
5. The user agreed to the authorization and closed the authorization when he was busy
usage method:
public class MainActivity extends PermissionActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkPermissionCAMERA(new OnPermissionRequestListener() { @Override public void onPermissionHave() { showToast("User agreed to camera rights"); checkPermissionWRITE_EXTERNAL_STORAGE(new OnPermissionRequestListener() { @Override public void onPermissionHave() { showToast("User agreed to storage rights,You can take photos"); } @Override public void onPermissionRefuse() { showToast("Storage permission denied by user"); } @Override public void onPermissionRefuseNoAsk() { showToast("The user denied the storage permission and checked don't ask again"); } }); } @Override public void onPermissionRefuse() { showToast("Camera permission denied by user"); } @Override public void onPermissionRefuseNoAsk() { showToast("The user denied the camera permission and checked don't ask again"); } }); } }); //perhaps // checkPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, // "Storage permission is disabled", "photographing function must have storage permission turned on, otherwise it cannot be used", new OnPermissionRequestListener(){ // // @Override // public void onPermissionHave() { // // showToast("the user has agreed to the storage rights"); // // // } // // @Override // public void onPermissionRefuse() { // showToast("storage permission denied by user"); // // } // // @Override // public void onPermissionRefuseNoAsk() { // showToast("the user has denied the storage permission and has selected don't ask again"); // // } // }); } @Override public void onClick(View v) { } }
Source code:
public abstract class PermissionActivity extends AppCompatActivity implements View.OnClickListener { private String toast_perm_refuse,dialog_perm_refuse_noask; private OnPermissionRequestListener onPermissionHaveListener; public <T extends View> T getViewByID(@IdRes int id) { return (T) findViewById(id); } //?????????????????????????????????????????????????????????????????????? public String nullToString(Object object) { return object == null ? "" : object.toString(); } //?????????????????????????????????????????????????????????????????????? public void showToast(String msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } public void showToast(int string_id) { Toast.makeText(this, getResources().getString(string_id), Toast.LENGTH_SHORT).show(); } public void startAppcompatActivity(Class<?> cls) { startActivity(new Intent(this, cls)); } /* 6.0 Permission check storage permission */ public void checkPermissionWRITE_EXTERNAL_STORAGE( OnPermissionRequestListener onPermissionHaveListener) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); this.toast_perm_refuse = "Storage rights disabled"; this.dialog_perm_refuse_noask="Storage permission is disabled, you will not be able to use camera, album, picture download and other functions"; this.onPermissionHaveListener = onPermissionHaveListener; } /* 6.0 Permission check camera permission */ public void checkPermissionCAMERA( OnPermissionRequestListener onPermissionHaveListener) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1); this.toast_perm_refuse = "Camera permission disabled"; this.dialog_perm_refuse_noask="Camera permission is disabled, you will not be able to take photos with the camera"; this.onPermissionHaveListener = onPermissionHaveListener; } /* 6.0 Permission check */ public void checkPermission(String[] permission, String toast_perm_refuse,String dialog_perm_refuse_noask, OnPermissionRequestListener onPermissionHaveListener) { ActivityCompat.requestPermissions(this, permission, 1); this.toast_perm_refuse = toast_perm_refuse; this.dialog_perm_refuse_noask=dialog_perm_refuse_noask; this.onPermissionHaveListener = onPermissionHaveListener; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); for (int i = 0; i < permissions.length; i++) { if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { if (onPermissionHaveListener != null) { onPermissionHaveListener.onPermissionHave(); } continue; } if (grantResults[i] == PackageManager.PERMISSION_DENIED) { //If the shouldshowrequestpermissionrational returns false when the user has denied authorization // It can be inferred that the user has selected the "do not prompt" option. In this case, the user needs to be guided to the settings page for manual authorization if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) { if (onPermissionHaveListener != null) { onPermissionHaveListener.onPermissionRefuseNoAsk(); } //Explain the reason and guide the user to the settings page for manual authorization new AlertDialog.Builder(this) .setMessage(dialog_perm_refuse_noask) .setPositiveButton("De authorisation", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Guide the user to the settings page for manual authorization Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getApplicationContext().getPackageName(), null); intent.setData(uri); startActivity(intent); } }) .setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Guide user to authorize manually, permission request failed } }).show(); } else { //Permission request failed, but no "don't prompt again" option selected showToast(toast_perm_refuse); if (onPermissionHaveListener != null) { onPermissionHaveListener.onPermissionRefuse(); } } break; } } } public interface OnPermissionRequestListener { public void onPermissionHave(); public void onPermissionRefuse(); public void onPermissionRefuseNoAsk(); } }
Focus on topics Common open source libraries for Android Development
WeChat public address
QQ group