Android权限处理

Android 6.0 权限分级

正常权限 :

涵盖应用需要访问其沙盒外部数据或资源,但对用户隐私或其他应用操作风险很小的区域。这些权限在应用安装时授予,运行时不再询问用户。例如: 网络访问、WIFI状态、音量设置等。

危险权限:

涵盖应用需要涉及用户隐私信息的数据或资源,或者可能对用户存储的数据或其他应用的操作产生影响的区域。例如: 读取通讯录、读写存储器数据、获取用户位置等。如果应用声明需要这些危险权限,则必须在运行时明确告诉用户,让用户手动授予。例如相机权限,获取内存权限

Demo

PermissionBean:

/**
 * 权限bean
 */

public class PermissionBean {

    //是否已授权
    private boolean isGranted ;
    //权限名字
    private String name;

    public PermissionBean(boolean isGranted, String name) {
        this.isGranted = isGranted;
        this.name = name;
    }

    public boolean isGranted() {
        return isGranted;
    }

    public void setGranted(boolean granted) {
        isGranted = granted;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

PermissonAty

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
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.ArrayList;
import java.util.List;

/**
 * 权限轮子
 * 需要再Manifest中:
 * 
 * 
 */

public class PermissonAty extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_quanxian);
        Button button = (Button) findViewById(R.id.btn_get);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initGetPermission();
            }
        });
        Button button2 = (Button) findViewById(R.id.btn_get_2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPermission();
            }
        });

    }

    /**
     * 初始化获取权限
     */
    private void initGetPermission() {

        List permissionBeenList = new ArrayList<>();
        //相机权限
        permissionBeenList.add(new PermissionBean(false, Manifest.permission.CAMERA));
        //写入内存卡权限
        permissionBeenList.add(new PermissionBean(false, Manifest.permission.WRITE_EXTERNAL_STORAGE));
        List needRequestPermission = new ArrayList<>();

        //检测有没有授权
        for (int i = 0; i < permissionBeenList.size(); i++) {
            if (ContextCompat.checkSelfPermission(this, permissionBeenList.get(i).getName())
                    == PackageManager.PERMISSION_GRANTED) {
                permissionBeenList.get(i).setGranted(true);
            }
        }

        //遍历出 需要的权限
        for (int i = 0; i < permissionBeenList.size(); i++) {
            if (!permissionBeenList.get(i).isGranted()) {
                needRequestPermission.add(permissionBeenList.get(i).getName());
            }
        }

        Log.v("Lin", needRequestPermission.toString());

        if (needRequestPermission.size() == 0) {
            Log.v("Lin", "全部已经授权");
        } else {
            Log.v("Lin", "还有没有授权的");
            ActivityCompat.requestPermissions(this, needRequestPermission.toArray(new String[needRequestPermission.size()]), 100);
        }

    }
    //指定的位置获取权限
    private void getPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            Log.v("Lin", "没授权");
            if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.CAMERA)) {
                Toast.makeText(this, "没有获取相机权限", Toast.LENGTH_SHORT).show();
            } else {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.CAMERA},
                        100);
            }
        } else {
            Log.v("Lin", "已经授权");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 100) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.v("Lin", "权限已被授予");
            } else {
                Log.v("Lin", "权限没被授予");
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

}

你可能感兴趣的:(Android权限处理)