EasyPermissions的使用方法

从 Android 6.0(API 级别 23)开始,用户开始在应用运行时向其授予权限,而不是在应用安装时授予。

系统权限分为两类:正常权限和危险权限:
- 正常权限不会直接给用户隐私权带来风险。如果您的应用在其清单中列出了正常权限,系统将自动授予该权限。
- 危险权限会授予应用访问用户机密数据的权限。如果您的应用在其清单中列出了正常权限,系统将自动授予该权限。如果您列出了危险权限,则用户必须明确批准您的应用使用这些权限。

1.Google原始的请求权限的方法

   //正常获取权限
    private void checkPermissionForNormal() {
        //判断是否同意此权限
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {           
                //申请权限
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 100);
            }
        } else {
            Toast.makeText(this, "已授权 CALL_PHONE", Toast.LENGTH_LONG).show();
        }
    }

权限的回调

 @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 100: {
                //返回的结果数组大于0说明有结果
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "同意了权限", Toast.LENGTH_SHORT).show();
                    callPhone();
                } else {
                    Toast.makeText(this, "拒绝了权限", Toast.LENGTH_SHORT).show();
                }
                return;
            }
        }
    }

2.封装的三方库——EasyPermissions
这个用于权限的申请,使用起来十分的简单

  • 引入库
compile 'pub.devrel:easypermissions:0.1.5'  
  • 代码块
package com.example.easypermissions_demo;

import android.Manifest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.List;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,EasyPermissions.PermissionCallbacks {

    private static final String TAG = "MainActivity";

    private static final int CAMERA_REQUESTCODE = 101;
    private static final int LOCATION_CONTACTS_REQUESTCODE = 102;

    private Button button_camera;
    private Button button_location_and_contacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button_camera = (Button) findViewById(R.id.button_camera);
        button_location_and_contacts = (Button) findViewById(R.id.button_location_and_contacts);
        button_camera.setOnClickListener(this);
        button_location_and_contacts.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_camera:
                requestCamera();
                break;
            case R.id.button_location_and_contacts:
                requestLocationAndContacts();
                break;
        }
    }

    public void requestCamera() {
        if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
            Toast.makeText(this, "已授权 Camera", Toast.LENGTH_LONG).show();
        } else {
            // request for one permission
            EasyPermissions.requestPermissions(this, getString(R.string.rationale_camera),
                    CAMERA_REQUESTCODE, Manifest.permission.CAMERA);
        }
    }

    public void requestLocationAndContacts() {
        String[] perms = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_CONTACTS };
        if (EasyPermissions.hasPermissions(this, perms)) {
            Toast.makeText(this, "已授权 联系人和 位置信息", Toast.LENGTH_LONG).show();
        } else {
            // request for both permissions
            EasyPermissions.requestPermissions(this, getString(R.string.rationale_location_contacts),
                    LOCATION_CONTACTS_REQUESTCODE, perms);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // EasyPermissions 权限处理请求结果
        Log.i(TAG,"onRequestPermissionsResult:"+ requestCode);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    //同意授权
    @Override
    public void onPermissionsGranted(int requestCode, List perms) {
        Log.d(TAG, "onPermissionsGranted:" + requestCode + ":" + perms.size());
    }

    //拒绝授权
    @Override
    public void onPermissionsDenied(int requestCode, List perms) {
        Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());
    }
}

你可能感兴趣的:(笔记)