6.0动态权限

package com.aqinga.my;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    public static final String quanxian = Manifest.permission.CAMERA;
    public static final int CAMERA_PERMISSION_REQUEST_CODE = 100;
    private static final int a = 10;
    public static String TAG = "TAG";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void XiangJi(View view) {
        if (ContextCompat.checkSelfPermission(this,quanxian)== PackageManager.PERMISSION_GRANTED){
            QiDong();
            Log.e(TAG,"检查权限======用户已经拥有这个权限了");
        }else {
            ActivityCompat.requestPermissions(this,new String[]{quanxian},CAMERA_PERMISSION_REQUEST_CODE);
            Log.e(TAG,"检查权限======用户拒绝了这个权限");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case a:
                if (ContextCompat.checkSelfPermission(this,quanxian)==PackageManager.PERMISSION_GRANTED){
                    QiDong();
                    Log.e(TAG, "===========设置页面返回之后-再次检查权限---用户已经拥有相机这个权限了");
                }else {
                    Log.e(TAG, "===========设置页面返回之后-再次检查权限---用户没有开启这个权限,在这不用再去请求权限了");
                }
                break;
        }
    }

    private void QiDong() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,0);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case CAMERA_PERMISSION_REQUEST_CODE:
                if (permissions[0].equals(quanxian)){
                    if (grantResults[0]==PackageManager.PERMISSION_GRANTED){
                        Log.e(TAG, "===========权限回调---用户同意了");
                        QiDong();
                    }else {
                        Log.e(TAG, "===========权限回调---用户拒绝了");
                        if (ActivityCompat.shouldShowRequestPermissionRationale(this,quanxian)){
                            Shezhi();
                        }else {
                            showTipExplainPermission();
                        }
                    }
                }
                break;
        }
    }

    private void Shezhi() {
        new     AlertDialog.Builder(this)
                .setTitle("需要打开相机权限")
                .setMessage("再设置权限中打开相机权限")
                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        GotoShezhi();
                    }
                })
                .setNegativeButton("取消",null)
                .show();
    }


    private void GotoShezhi() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        intent.setData(uri);
        startActivityForResult(intent,a);
    }

    private void showTipExplainPermission() {
        new AlertDialog.Builder(this)
                .setTitle("说明")
                .setMessage("需要开启相机权限去拍照")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Shezhi();
                    }
                })
                .setNegativeButton("取消",null)
                .show();
    }
}

你可能感兴趣的:(android-studio)