|
检测是否同意权限
public boolean checkPermission(Context context, String permission) { if (context != null) { return ContextCompat.checkSelfPermission(context, permission) == 0; } else { Log.d(this.PERMISSONTAG, "check permission context is null"); return true; }}
申请权限
public interface PermissionCallback { void success(); void error();}private boolean isMarshmallow() { return VERSION.SDK_INT >= 23;}public void requestPermission(Context context, String permission, int requestCode, PermissionCallback callback) { if (!this.isMarshmallow()) { Log.d(this.PERMISSONTAG, "Not Android 6.0 Above"); callback.success(); } else { this.requestPermissionCode = requestCode; this.newCallback = callback; this.requestPermission = permission; this.permissions = new String[]{permission}; this.context = context; if (!this.checkPermission(context, permission)) { ActivityCompat.requestPermissions((Activity)context, this.permissions, requestCode); } else { this.newCallback.success(); } }} |
|