安卓6.0申请权限 AndPermission

https://github.com/yanzhenjie/AndPermission

compile 'com.yanzhenjie.alertdialog:alertdialog:1.0.1'
implementation 'com.yanzhenjie:permission:2.0.0-rc3'

1,DefaultRationale

/*
 * Copyright © Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.vily.imagescandemo2;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;

import com.yanzhenjie.alertdialog.AlertDialog;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RequestExecutor;

import java.util.List;

/**
 * Created by YanZhenjie on 2018/1/1.
 */
public final class DefaultRationale implements Rationale {

    @Override
    public void showRationale(Context context, List<String> permissions, final RequestExecutor executor) {
        List<String> permissionNames = Permission.transformText(context, permissions);
        String message = context.getString(R.string.message_permission_rationale, TextUtils.join("\n", permissionNames));

        AlertDialog.newBuilder(context)
                .setCancelable(false)
                .setTitle(R.string.title_dialog)
                .setMessage(message)
                .setPositiveButton(R.string.resume, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        executor.execute();
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        executor.cancel();
                    }
                })
                .show();
    }
}

2,PermissionSetting

/*
 * Copyright © Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.vily.imagescandemo2;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;

import com.yanzhenjie.alertdialog.AlertDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.SettingService;

import java.util.List;

/**
 * Created by YanZhenjie on 2018/1/1.
 */
public final class PermissionSetting {

    private final Context mContext;

    public PermissionSetting(Context context) {
        this.mContext = context;
    }

    public void showSetting(final List<String> permissions) {
        List<String> permissionNames = Permission.transformText(mContext, permissions);
        String message = mContext.getString(R.string.message_permission_always_failed, TextUtils.join("\n", permissionNames));

        final SettingService settingService = AndPermission.permissionSetting(mContext);
        AlertDialog.newBuilder(mContext)
                .setCancelable(false)
                .setTitle(R.string.title_dialog)
                .setMessage(message)
                .setPositiveButton(R.string.setting, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        settingService.execute();
                    }
                })
                .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        settingService.cancel();
                    }
                })
                .show();
    }
}

3,使用

 // 6.0之上申请牌照和写入存储权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            AndPermission.with(Select_or_Picture_Activity.this).permission(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .rationale(new DefaultRationale() )
                    .onGranted(new Action() {
                        @Override
                        public void onAction(List<String> permissions) {
                            System.out.println("---------有权限了");
                            Toast.makeText(Select_or_Picture_Activity.this,R.string.successfully,Toast.LENGTH_SHORT).show();
                        }
                    })
                    .onDenied(new Action() {
                        @Override
                        public void onAction(List<String> permissions) {
                            System.out.println("---------没有权限");
                            Toast.makeText(Select_or_Picture_Activity.this,R.string.failure,Toast.LENGTH_SHORT).show();
                            if (AndPermission.hasAlwaysDeniedPermission(Select_or_Picture_Activity.this, permissions)) {
                                mSetting.showSetting(permissions);
                            }
                        }
                    }).start();
        }else {
//            new getPic().execute(); // 获取图片
        }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Anroid6.0权限问题 apk22之后Android把一些涉及用户个人信息的权限都做了默认没有权限处理,需要用户确认才可以: 用户不需要在安装软件的时候一次性授权所有申请权限,而是可以在软件的使用过程中再对某一项权限申请进行授权。举例说明:一款相机应用在运行时申请了地理位置定位权限,就算我拒绝了这个权限,我仍然可以使用这款应用的其他功能,不用像6.0之前无法安装它。 解决办法: ● 投机取巧 如果我们不想使用6.0或者7.0的新特性,那么我们把targetSdkVersion设置22,就可以很好的避开动态配置运行时权限。而targetSdkVersion设置为22,并不影响其在Android 6.0或7.机制上使用,因为高版本兼容低版本。 解决办法:(简单到没朋友) AndroidAcp ● Acp 为 Android check permission 缩写,此库简化Android 6.0 系统复杂的权限操作而编写。 特点 ● 支持批量权限申请,不需要重写 onRequestPermissionsResult 方法,Activity 与 Fragment 中用法一致,一句话搞定。 ● 处理权限拒绝,或勾选不再询问,导致不能正常使用功能的提示框,支持跳转设置权限界面开启权限,所有提示框文字可自定义。 ● #使用Gradle构建时添加一下依赖即可: compile 'com.mylhyl:acp:1.1.7' 怎麽用? Acp.getInstance(this).request(new AcpOptions.Builder() .setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE , Manifest.permission.READ_PHONE_STATE , Manifest.permission.SEND_SMS) /*以下为自定义提示语、按钮文字 .setDeniedMessage() .setDeniedCloseBtn() .setDeniedSettingBtn() .setRationalMessage() .setRationalBtn()*/ .build(), new AcpListener() { @Override public void onGranted() { writeSD(); getIMEI(); } @Override public void onDenied(List permissions) { makeText(permissions.toString() + "权限拒绝"); } }); ***************************** 二維碼的使用: ZXING ● 可打开默认二维码扫描页面 ● 支持对图片Bitmap的扫描功能 ● 支持对UI的定制化操作 ● 支持对条形码的扫描功能 ● 支持生成二维码操作 ● 支持控制闪光灯开关 ● 集成默认的二维码扫描页面 在具体介绍该扫描库之前我们先看一下其具体的使用方式,看看是不是几行代码就可以集成二维码扫描的功能。 ● 在module的build.gradle中执行compile操作 compile 'cn.yipianfengye.android:zxing-library:2.1' ● 1 ● 1 ● 在Application中执行初始化操作 @Override public void onCreate() { super.onCreate(); ZXingLibrary.
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值