1. 程式人生 > >動態獲取網路許可權

動態獲取網路許可權

//直接寫一個類

public class PermissionUtils {

    public static void permission(final Context context, final PermissionListener listener){
        AndPermission.with(context)
                .permission(Permission.CAMERA, Permission.READ_EXTERNAL_STORAGE,Permission.WRITE_EXTERNAL_STORAGE)
                .onGranted(new Action() {
                    @Override
                    public void onAction(List<String> permissions) {
                        listener.success();
                    }
                })
                .onDenied(new Action() {
                    @Override
                    public void onAction(List<String> permissions) {
                        Uri packageURI = Uri.parse("package:" + context.getPackageName());
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                        Toast.makeText(context, "沒有許可權無法掃描呦", Toast.LENGTH_LONG).show();
                    }
                }).start();
    }

    public interface PermissionListener{
        void success();
    }
}