Android技术Android 权限程序员

Android6.0运行权限封装

2017-03-05  本文已影响353人  蓝枫zeke

简介

Android6.0中对权限分为了一般权限和危险权限。

危险权限

关于日历的权限:

<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>

关于相机的权限:

<uses-permission android:name="android.permission.CAMERA"/>

关于联系人的权限:

<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.GET_CONTACTS"/>

关于位置的权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

关于电话的权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.USE_SIP"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

关于传感器的权限:

<uses-permission android:name="android.permission.BODY_SENSORS"/>

关于短信的权限:

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH"/>
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.READ_CELL_BROADCASTS"/>

关于SD卡的权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

实例说明

先上两张图:

x.png y.png

BasePermissionsActivity.java


package com.zhoujian.permissions;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
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.util.Log;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhoujian on 2017/3/5.
 */

public class BasePermissionsActivity extends Activity
{


    public static final String TAG = "BasePermissionsActivity";

    public static  int REQUEST_CODE = 0;

    public void requestPermission(String[] permissions,int requestCode)
    {

        this.REQUEST_CODE = requestCode;

        //检查权限是否授权
        if(checkPermissions(permissions))
        {
            permissinSucceed(REQUEST_CODE);
        }
        else
        {
            List<String> needPermissions = getPermissions(permissions);
            ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), REQUEST_CODE);
        }
    }

    private List<String> getPermissions(String[] permissions)
    {
        List<String> permissionList = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) !=
                    PackageManager.PERMISSION_GRANTED ||
                    ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                permissionList.add(permission);
            }
        }
        return permissionList;

    }

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


        if (requestCode == REQUEST_CODE) {
            if (verificationPermissions(grantResults)) {
                permissinSucceed(REQUEST_CODE);
            } else {
                permissionFailing(REQUEST_CODE);
                showFaiingDialog();
            }
        }
    }

    private boolean verificationPermissions(int[] results)
    {

        for (int result : results) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;

    }

    /**
     * 检测所有的权限是否都已授权
     *
     * @param permissions
     * @return
     */
    private boolean checkPermissions(String[] permissions)
    {
        if(Build.VERSION.SDK_INT<Build.VERSION_CODES.M)
        {
            return true;
        }

        for(String permission:permissions)
        {

            if(ContextCompat.checkSelfPermission(BasePermissionsActivity.this,permission)!= PackageManager.PERMISSION_GRANTED)
            {
                return false;
            }
        }
        return true;
    }


    private void showFaiingDialog()
    {

        new AlertDialog.Builder(this)
                .setTitle("消息")
                .setMessage("当前应用无此权限,该功能暂时无法使用。如若需要,请单击确定按钮进行权限授权!")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        return;
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startSettings();
                    }
                }).show();

    }

    private void startSettings() {

        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }

    public void permissionFailing(int code) {

        Log.d(TAG, "获取权限失败=" + code);
    }

    public void permissinSucceed(int code) {

        Log.d(TAG, "获取权限成功=" + code);
    }
}

MainActivity.java

package com.zhoujian.permissions;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;

public class MainActivity extends BasePermissionsActivity
{

    private Intent intent;
    private Button mTake_phone;
    private Button mTake_photo;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        clickEvents();
    }

    private void initViews()
    {
        mTake_phone = (Button)findViewById(R.id.take_phone);
        mTake_photo = (Button)findViewById(R.id.take_photo);
    }

    private void clickEvents()
    {

        mTake_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                requestPermission(new String[]{Manifest.permission.CALL_PHONE},1);
            }
        });


        mTake_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                requestPermission(new String[]{Manifest.permission.CAMERA},2);
            }
        });

    }


    /**
     * 权限成功回调函数
     *
     * @param requestCode
     */
    @Override
    public void permissinSucceed(int requestCode) {
        super.permissinSucceed(requestCode);
        switch (requestCode) {
            case 1:
                intent= new Intent(Intent.ACTION_CALL, Uri.parse("tel:15855759639"));
                startActivity(intent);
                break;

            case 2:
                intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivity(intent);
                break;

        }

    }

    @Override
    public void permissionFailing(int code) {
        super.permissionFailing(code);

    }
}

清单文件加上权限

<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.CAMERA"/>

上一篇下一篇

猜你喜欢

热点阅读