Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android開發 權限適配6.0 permissionsdispatcher的安裝使用

android開發 權限適配6.0 permissionsdispatcher的安裝使用

編輯:關於Android編程

折騰了一陣,終於是安裝上了,臥槽,先在AndroidStudio裡面安裝permissiondispatcher插件,看圖:
這裡寫圖片描述
重啟完成之後就是配置build.gradle,看圖便知..
這裡寫圖片描述
這裡寫圖片描述
好了,配置完成
—————–2016年8月26日01:48:35—————————-
現在開始使用(S申請單個權限 -拍照的-):<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPrK8vta0+sLrIGFjdGl2aXR5X3JlcXVlc3RfcGVybWlzc2lvbl9zaW5nbGUueG1so7o8L3A+DQo8cHJlIGNsYXNzPQ=="brush:java;"> --------------------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




    

—————-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上面測試均有提示

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved