com.hjq.permissions.Permission Java Examples

The following examples show how to use com.hjq.permissions.Permission. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UpdateDialog.java    From AndroidProject with Apache License 2.0 6 votes vote down vote up
/**
 * 安装 Apk
 */
@Permissions({Permission.REQUEST_INSTALL_PACKAGES})
private void installApk() {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        uri = FileProvider.getUriForFile(getContext(), AppConfig.getPackageName() + ".provider", mApkFile);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    } else {
        uri = Uri.fromFile(mApkFile);
    }

    intent.setDataAndType(uri, "application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getContext().startActivity(intent);
}
 
Example #2
Source File: CameraActivity.java    From AndroidProject with Apache License 2.0 6 votes vote down vote up
@DebugLog
@Permissions({Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE, Permission.CAMERA})
public static void start(BaseActivity activity, OnCameraListener listener) {
    File file = createCameraFile();
    Intent intent = new Intent(activity, CameraActivity.class);
    intent.putExtra(IntentKey.FILE, file);
    activity.startActivityForResult(intent, (resultCode, data) -> {

        if (listener == null) {
            return;
        }

        if (resultCode == RESULT_OK) {
            listener.onSelected(file);
        } else {
            listener.onCancel();
        }
    });
}
 
Example #3
Source File: PhotoActivity.java    From AndroidProject with Apache License 2.0 6 votes vote down vote up
@DebugLog
@Permissions({Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE})
public static void start(BaseActivity activity, int maxSelect, OnPhotoSelectListener listener) {
    if (maxSelect < 1) {
        // 最少要选择一个图片
        throw new IllegalArgumentException("are you ok?");
    }
    Intent intent = new Intent(activity, PhotoActivity.class);
    intent.putExtra(IntentKey.AMOUNT, maxSelect);
    activity.startActivityForResult(intent, (resultCode, data) -> {

        if (listener == null || data == null) {
            return;
        }

        if (resultCode == RESULT_OK) {
            listener.onSelected(data.getStringArrayListExtra(IntentKey.PICTURE));
        } else {
            listener.onCancel();
        }
    });
}
 
Example #4
Source File: MainActivity.java    From XToast with Apache License 2.0 5 votes vote down vote up
public void show6(View v) {
    XXPermissions.with(this)
            .permission(Permission.SYSTEM_ALERT_WINDOW)
            .request(new OnPermission() {

                @Override
                public void hasPermission(List<String> granted, boolean isAll) {

                    // 传入 Application 表示这个是一个全局的 Toast
                    new XToast(getApplication())
                            .setView(R.layout.toast_phone)
                            .setGravity(Gravity.END | Gravity.BOTTOM)
                            .setXOffset(100)
                            .setYOffset(100)
                            // 设置指定的拖拽规则
                            .setDraggable(new SpringDraggable())
                            .setOnClickListener(android.R.id.icon, new OnClickListener<ImageView>() {

                                @Override
                                public void onClick(XToast toast, ImageView view) {
                                    // 点击后跳转到拨打电话界面
                                    Intent intent = new Intent(Intent.ACTION_DIAL);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    toast.startActivity(intent);
                                }
                            })
                            .show();
                }

                @Override
                public void noPermission(List<String> denied, boolean quick) {
                    new XToast(MainActivity.this)
                            .setDuration(1000)
                            .setView(R.layout.toast_hint)
                            .setImageDrawable(android.R.id.icon, R.mipmap.ic_dialog_tip_error)
                            .setText(android.R.id.message, "请先授予悬浮窗权限")
                            .show();
                }
            });
}
 
Example #5
Source File: MainActivity.java    From EasyHttp with Apache License 2.0 5 votes vote down vote up
@Override
protected void onRestart() {
    super.onRestart();
    if (XXPermissions.hasPermission(this, Permission.Group.STORAGE)) {
        hasPermission(null, true);
    } else {
        requestPermission();
    }
}
 
Example #6
Source File: MainActivity.java    From EasyHttp with Apache License 2.0 5 votes vote down vote up
/**
 * 安装 Apk
 */
private void installApk(final Context context, final File file) {
    XXPermissions.with(MainActivity.this)
            // 安装包权限
            .permission(Permission.REQUEST_INSTALL_PACKAGES)
            .request(new OnPermission() {
                @Override
                public void hasPermission(List<String> granted, boolean isAll) {
                    if (isAll) {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        Uri uri;
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
                            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                        } else {
                            uri = Uri.fromFile(file);
                        }

                        intent.setDataAndType(uri, "application/vnd.android.package-archive");
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    }
                }

                @Override
                public void noPermission(List<String> denied, boolean quick) {

                }
            });
}
 
Example #7
Source File: CameraActivity.java    From AndroidProject with Apache License 2.0 5 votes vote down vote up
@Override
protected void initData() {
    // 启动系统相机
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (XXPermissions.isHasPermission(this, Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE, Permission.CAMERA)
            && intent.resolveActivity(getPackageManager()) != null) {
        mFile = getSerializable(IntentKey.FILE);
        if (mFile != null && mFile.exists()) {

            Uri imageUri;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                // 通过 FileProvider 创建一个 Content 类型的 Uri 文件
                imageUri = FileProvider.getUriForFile(this, AppConfig.getPackageName() + ".provider", mFile);
            } else {
                imageUri = Uri.fromFile(mFile);
            }
            // 对目标应用临时授权该 Uri 所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            // 将拍取的照片保存到指定 Uri
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, CAMERA_REQUEST_CODE);
        } else {
            toast(R.string.photo_picture_error);
            finish();
        }
    } else {
        toast(R.string.photo_launch_fail);
        finish();
    }
}
 
Example #8
Source File: MainActivity.java    From XXPermissions with Apache License 2.0 5 votes vote down vote up
public void requestPermission(View view) {
    XXPermissions.with(this)
            // 可设置被拒绝后继续申请,直到用户授权或者永久拒绝
            //.constantRequest()
            // 支持请求6.0悬浮窗权限8.0请求安装权限
            //.permission(Permission.REQUEST_INSTALL_PACKAGES)
            // 不指定权限则自动获取清单中的危险权限
            .permission(Permission.Group.STORAGE, Permission.Group.CALENDAR)
            .request(new OnPermission() {

                @Override
                public void hasPermission(List<String> granted, boolean all) {
                    if (all) {
                        ToastUtils.show("获取权限成功");
                    }else {
                        ToastUtils.show("获取权限成功,部分权限未正常授予");
                    }
                }

                @Override
                public void noPermission(List<String> denied, boolean quick) {
                    if(quick) {
                        ToastUtils.show("被永久拒绝授权,请手动授予权限");
                        //如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(MainActivity.this);
                    } else {
                        ToastUtils.show("获取权限失败");
                    }
                }
            });
}
 
Example #9
Source File: MainActivity.java    From XXPermissions with Apache License 2.0 5 votes vote down vote up
public void hasPermission(View view) {
    if (XXPermissions.hasPermission(MainActivity.this, Permission.Group.STORAGE)) {
        ToastUtils.show("已经获取到权限,不需要再次申请了");
    }else {
        ToastUtils.show("还没有获取到权限或者部分权限未授予");
    }
}
 
Example #10
Source File: MainActivity.java    From EasyHttp with Apache License 2.0 4 votes vote down vote up
private void requestPermission() {
    XXPermissions.with(this)
            .permission(Permission.Group.STORAGE)
            .request(this);
}
 
Example #11
Source File: TestFragmentC.java    From AndroidProject with Apache License 2.0 4 votes vote down vote up
@SingleClick
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_test_image1:
            mImageView.setVisibility(View.VISIBLE);
            GlideApp.with(this)
                    .load("https://www.baidu.com/img/bd_logo.png")
                    .into(mImageView);
            break;
        case R.id.btn_test_image2:
            mImageView.setVisibility(View.VISIBLE);
            GlideApp.with(this)
                    .load("https://www.baidu.com/img/bd_logo.png")
                    .circleCrop()
                    .into(mImageView);
            break;
        case R.id.btn_test_image3:
            mImageView.setVisibility(View.VISIBLE);
            GlideApp.with(this)
                    .load("https://www.baidu.com/img/bd_logo.png")
                    .transform(new RoundedCorners((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, this.getResources().getDisplayMetrics())))
                    .into(mImageView);
            break;
        case R.id.btn_test_image4:
            PhotoActivity.start(getAttachActivity(), new PhotoActivity.OnPhotoSelectListener() {

                @Override
                public void onSelected(List<String> data) {
                    mImageView.setVisibility(View.VISIBLE);
                    GlideApp.with(getAttachActivity())
                            .load(data.get(0))
                            .into(mImageView);
                }

                @Override
                public void onCancel() {
                    toast("取消了");
                }
            });
            break;
        case R.id.btn_test_toast:
            toast("我是吐司");
            break;
        case R.id.btn_test_permission:
            XXPermissions.with(getAttachActivity())
                    // 可设置被拒绝后继续申请,直到用户授权或者永久拒绝
                    //.constantRequest()
                    // 支持请求6.0悬浮窗权限8.0请求安装权限
                    //.permission(Permission.SYSTEM_ALERT_WINDOW, Permission.REQUEST_INSTALL_PACKAGES)
                    // 不指定权限则自动获取清单中的危险权限
                    .permission(Permission.CAMERA)
                    .request(new OnPermission() {

                        @Override
                        public void hasPermission(List<String> granted, boolean isAll) {
                            if (isAll) {
                                toast("获取权限成功");
                            } else {
                                toast("获取权限成功,部分权限未正常授予");
                            }
                        }

                        @Override
                        public void noPermission(List<String> denied, boolean quick) {
                            if(quick) {
                                toast("被永久拒绝授权,请手动授予权限");
                                //如果是被永久拒绝就跳转到应用权限系统设置页面
                                XXPermissions.gotoPermissionSettings(getAttachActivity());
                            } else {
                                toast("获取权限失败");
                            }
                        }
                    });
            break;
        case R.id.btn_test_setting:
            XXPermissions.gotoPermissionSettings(getAttachActivity());
            break;
        case R.id.btn_test_state_black:
            if (getAttachActivity().getStatusBarConfig() != null) {
                getAttachActivity().getStatusBarConfig().statusBarDarkFont(true).init();
            }
            break;
        case R.id.btn_test_state_white:
            if (getAttachActivity().getStatusBarConfig() != null) {
                getAttachActivity().getStatusBarConfig().statusBarDarkFont(false).init();
            }
            break;
        default:
            break;
    }
}