安卓6.0及以上权限处理

Posted Gradle官方文件

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓6.0及以上权限处理相关的知识,希望对你有一定的参考价值。

自己封装的一个基Activity去处理安卓M权限的问题。

源码地址:
https://github.com/anzhuojinjie/Permission

向系统请求权限

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.support.v7.app.AppCompatActivity;
import android.util.Log;

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

/**
 * Created by Joe on 2016/11/29.
 */

public class MPermissionsActivity extends AppCompatActivity 
    private final String TAG = "MPermissions";
    private int REQUEST_CODE_PERMISSIONS = 0x00099;

    /**
     * 向系统请求权限
     * @param permissions   请求的权限
     * @param requestCode   请求权限的请求码
     */
    public void requestPermission(String[] permissions,int requestCode)
        this.REQUEST_CODE_PERMISSIONS = requestCode;
        if (checkPermission(permissions))
            successPermission(REQUEST_CODE_PERMISSIONS);
        else 
            List<String> needPermissions = getDeniedPermissons(permissions);
            ActivityCompat.requestPermissions(this,needPermissions.toArray(new String[needPermissions.size()]),REQUEST_CODE_PERMISSIONS);
        
    

    /**
     * 检查是否已经被授予权限
     * @param permissions
     * @return
     */
    private boolean checkPermission(String[] permissions)
        if (Build.VERSION.SDK_INT<Build.VERSION_CODES.M)
            return true;
        
        for (String permission : permissions)
            if (ContextCompat.checkSelfPermission(this,permission) != PackageManager.PERMISSION_GRANTED)
                return false;
            
        
        return true;
    

    /**
     * 获取权限集中需要申请权限的列表
     * @param permissions
     * @return
     */
    private List<String> getDeniedPermissons(String[] permissions)
        List<String> needRequestPermissionList = new ArrayList<>();
        for (String permission : permissions)
            if (ContextCompat.checkSelfPermission(this,permission) != PackageManager.PERMISSION_GRANTED || ActivityCompat.shouldShowRequestPermissionRationale(this,permission))
                needRequestPermissionList.add(permission);
            
        
        return needRequestPermissionList;
    

    /**
     * 系统请求权限回调
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) 
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_PERMISSIONS)
            if (verifyPermissions(grantResults))
                successPermission(REQUEST_CODE_PERMISSIONS);
            else 
                failPermission(REQUEST_CODE_PERMISSIONS);
                showTipsDialog();
            
        
    
    private void showTipsDialog()
        new AlertDialog.Builder(this)
                .setTitle("提示信息")
                .setMessage("当前应用缺少必要权限,该功能暂时无法使用。如若需要,请单击【确定】按钮前往设置中心进行权限授权。")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() 
                    @Override
                    public void onClick(DialogInterface dialog, int which) 

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

    /**
     * 确认所有权限是否被授权
     * @param permissions
     * @return
     */
    private boolean verifyPermissions(int[] permissions)
        for (int permission : permissions)
            if (permission != PackageManager.PERMISSION_GRANTED)
                return false;
            
        
        return true;
    

    /**
     * 启动当前应用设置界面
     */
    private void startAppSetting()
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:"+getPackageName()));
        startActivity(intent);
    

    /**
     * 获取权限成功
     * @param requestCode
     */
    public void successPermission(int requestCode)
        Log.i(TAG, "获取权限成功= "+requestCode);
    

    /**
     * 获取权限失败
     * @param requestCode
     */
    public void failPermission(int requestCode)
        Log.i(TAG, "获取权限失败= "+requestCode);
    

使用:在相应的Activity继承MPermissionsActivity

public class MainActivity extends MPermissionsActivity 

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

    /**
     * 打电话
     *
     * @param view
     */
    public void onClick1(View view) 
        requestPermission(new String[]Manifest.permission.CALL_PHONE, 0x0001);
    

    /**
     * 写SD卡
     *
     * @param view
     */
    public void onClick2(View view) 
        requestPermission(new String[]Manifest.permission.WRITE_EXTERNAL_STORAGE, 0x0002);
    

    /**
     * 拍照
     *
     * @param view
     */
    public void onClick3(View view) 
        requestPermission(new String[]Manifest.permission.CAMERA, 0x0003);
    

    /**
     * 权限成功回调函数
     *
     * @param requestCode
     */
    @Override
    public void successPermission(int requestCode) 
        super.successPermission(requestCode);
        switch (requestCode) 
            case 0x0001:
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:15253052133"));
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) 
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                
                startActivity(intent);
                break;
        

    

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.hsg.permissionsdemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick1"
        android:text="打电话" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick2"
        android:text="写SD卡" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick3"
        android:text="拍照" />
</LinearLayout>

以上是关于安卓6.0及以上权限处理的主要内容,如果未能解决你的问题,请参考以下文章

在安卓6.0(及以上)设备上无法获取无线网卡MAC地址的解决方案

高德地图中安卓6.0权限处理方案

Android 6.0及以上版本号的执行时权限介绍

Unity在Android 6.0及以上版本弹出权限申请窗口的问题

安卓6.0动态授权

Android中如何使用代码打开各种类型的文件