android开发 权限适配6.0 permissionsdispatcher的安装使用

折腾了一阵,终于是安装上了,卧槽,先在AndroidStudio里面安装permissiondispatcher插件,看图:
这里写图片描述
重启完成之后就是配置build.gradle,看图便知..
这里写图片描述
这里写图片描述
好了,配置完成
—————–2016年8月26日01:48:35—————————-
现在开始使用(S申请单个权限 -拍照的-):

布局代码 activity_request_permission_single.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_call_phone"
        android:gravity="center"
        android:textSize="18sp"
        android:text="点击拍照"
        android:textColor="@android:color/white"
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>
--------------------RequestPermissionSingleActivity------------------------import android.Manifest;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.vincent.base.BaseActivity;
import com.vincent.utils.ToastUtils;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

/**
@RuntimePermissions 标记需要运行时判断的类
@NeedsPermission 标记需要检查权限的方法
@OnShowRationale 授权提示回调
@OnPermissionDenied 授权被拒绝回调
@OnNeverAskAgain 授权不再拒绝不再显示回调
* 一次性申请一个权限
* Created by Vincent on 2016/8/26.
*/
@RuntimePermissions
public class RequestPermissionSingleActivity extends BaseActivity implements View.OnClickListener{

private static final int CALL_CAMERA_REQUEST_CODE = 1;
private TextView tvCallPhone;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_request_permission_single);
    init();
}

private void init() {
    tvCallPhone=(TextView)findViewById(R.id.tv_call_phone);
    tvCallPhone.setOnClickListener(this);


}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.tv_call_phone:
            RequestPermissionSingleActivityPermissionsDispatcher.callPhoneWithCheck(this);
            break;
    }
}

/**
 * 方法前面不能带有private修饰符
 * @param phoneNum
 */
@NeedsPermission(Manifest.permission.CAMERA)
void callPhone() {
    Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(openCameraIntent, CALL_CAMERA_REQUEST_CODE);
}


@OnShowRationale(Manifest.permission.CAMERA)
void showRationaleForCamera(PermissionRequest request) {
    showRationaleDialog("使用此功能需要打开照相机的权限", request);
}

@OnPermissionDenied(Manifest.permission.CAMERA)
void onCameraDenied() {
    ToastUtils.defaultToast(this,"你拒绝了权限,该功能不可用");
}

@OnNeverAskAgain(Manifest.permission.CAMERA)
void onCameraNeverAskAgain() {
    ToastUtils.defaultToast(this,"不再允许询问该权限,该功能不可用");
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    RequestPermissionSingleActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}

private void showRationaleDialog(String messageResId, final PermissionRequest request) {
    new AlertDialog.Builder(this)
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(@NonNull DialogInterface dialog, int which) {
                    request.proceed();
                }
            })
            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(@NonNull DialogInterface dialog, int which) {
                    request.cancel();
                }
            })
            .setCancelable(false)
            .setMessage(messageResId)
            .show();
}

———————申请多个权限—————————–

布局文件:activity_request_permission_multiple.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_read_sms"
        android:gravity="center"
        android:textSize="18sp"
        android:text="读取短信息"
        android:textColor="@android:color/white"
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>

—————-RequestPermissionMultipleActivity——————-

import android.Manifest;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresPermission;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.vincent.base.BaseActivity;
import com.vincent.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

/**
 * 一次性申请多个权限
 * Created by Vincent on 2016/8/26.
 */
@RuntimePermissions
public class RequestPermissionMultipleActivity extends BaseActivity implements View.OnClickListener{
    private TextView tvReadSMS;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request_permission_multiple);
        init();
    }

    private void init() {
        tvReadSMS=(TextView)findViewById(R.id.tv_read_sms);
        tvReadSMS.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_read_sms:
                RequestPermissionMultipleActivityPermissionsDispatcher.readSMSWithCheck(this);
                break;
        }
    }
    动态申请权限相关操作/
    @NeedsPermission({Manifest.permission.READ_SMS, Manifest.permission.READ_EXTERNAL_STORAGE})
    void readSMS(){
       String sms= getSmsInPhone();
        Log.d("sms",sms);
    }
    @OnShowRationale({Manifest.permission.READ_SMS, Manifest.permission.READ_EXTERNAL_STORAGE})
    void noPermission(PermissionRequest request){
        showRationaleDialog("必须有权限才能使用", request);
    }
    @OnPermissionDenied({Manifest.permission.READ_SMS, Manifest.permission.READ_EXTERNAL_STORAGE})
    void juJue(){
        ToastUtils.defaultToast(this,"用户拒绝了申请");
    }
    @OnNeverAskAgain({Manifest.permission.READ_SMS, Manifest.permission.READ_EXTERNAL_STORAGE})
    void juJueAndNoTiShi(){
        ToastUtils.defaultToast(this,"拒绝且不提示");
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        RequestPermissionMultipleActivityPermissionsDispatcher.onRequestPermissionsResult(this,requestCode,grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public String getSmsInPhone()
    {
        final String SMS_URI_ALL = "content://sms/";
        final String SMS_URI_INBOX = "content://sms/inbox";
        final String SMS_URI_SEND = "content://sms/sent";
        final String SMS_URI_DRAFT = "content://sms/draft";

        StringBuilder smsBuilder = new StringBuilder();

        try{
            ContentResolver cr = getContentResolver();
            String[] projection = new String[]{"_id", "address", "person",
                    "body", "date", "type"};
            Uri uri = Uri.parse(SMS_URI_ALL);
            Cursor cur = cr.query(uri, projection, null, null, "date desc");

            if (cur.moveToFirst()) {
                String name;
                String phoneNumber;
                String smsbody;
                String date;
                String type;

                int nameColumn = cur.getColumnIndex("person");
                int phoneNumberColumn = cur.getColumnIndex("address");
                int smsbodyColumn = cur.getColumnIndex("body");
                int dateColumn = cur.getColumnIndex("date");
                int typeColumn = cur.getColumnIndex("type");

                do{
                    name = cur.getString(nameColumn);
                    phoneNumber = cur.getString(phoneNumberColumn);
                    smsbody = cur.getString(smsbodyColumn);

                    SimpleDateFormat dateFormat = new SimpleDateFormat(
                            "yyyy-MM-dd hh:mm:ss");
                    Date d = new Date(Long.parseLong(cur.getString(dateColumn)));
                    date = dateFormat.format(d);

                    int typeId = cur.getInt(typeColumn);
                    if(typeId == 1){
                        type = "接收";
                    } else if(typeId == 2){
                        type = "发送";
                    } else {
                        type = "";
                    }

                    smsBuilder.append("[");
                    smsBuilder.append(name+",");
                    smsBuilder.append(phoneNumber+",");
                    smsBuilder.append(smsbody+",");
                    smsBuilder.append(date+",");
                    smsBuilder.append(type);
                    smsBuilder.append("] ");

                    if(smsbody == null) smsbody = "";
                }while(cur.moveToNext());
            } else {
                smsBuilder.append("no result!");
            }

            smsBuilder.append("getSmsInPhone has executed!");
        } catch(SQLiteException ex) {
            Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
        }
        return smsBuilder.toString();
    }

    private void showRationaleDialog(String messageResId, final PermissionRequest request) {
        new AlertDialog.Builder(this)
                .setPositiveButton("好", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setNegativeButton("算了吧", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.cancel();
                    }
                })
                .setCancelable(false)
                .setMessage(messageResId)
                .show();
    }
}

效果图:

这里写图片描述

效果图有点卡,处理的不好,不管了
在我的华为荣耀6 android6.0上面测试均有提示,
哈,2016年8月26日01:52:00 时间不早了,先就这样咯
使用原生处理权限方式点这里:原生处理
——————–2016年8月26日08:39:02————————————

点击下载demo

——————–2016年9月15日09:57:09——————————-

这里写图片描述

如果出现这样的错误大概是引入了 下面这个包

import java.util.jar.Manifest;

删除就好了,需要的是这个:

import android.Manifest;

哈,两次看到这个错误突然才想起来这个,记下来
另外,写好了需要权限的方法之后需要编译一次才会出现相关的方法,。。
这里写图片描述
来一锤子

又一年中秋了,唉
—————————-2017年3月7日11:30:50—————————————-
最新版本为2.3.1
项目的build.gradle添加

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
        //classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

在app的build.gradle中添加

    apply plugin: 'com.android.application'
    apply plugin: 'android-apt'
    ...
    dependencies {
    ...
    compile 'com.github.hotchemi:permissionsdispatcher:2.3.1'
    apt 'com.github.hotchemi:permissionsdispatcher-processor:2.3.1'
    }
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值