图片 11

Android6.0动态权限申请步骤以及需要注意的一些坑

《安卓开发框架》系列文章
>>>

我的博客
我的博客:Android6.0动态权限申请步骤以及需要注意的一些坑

前言

几乎每个项目都会有各种各样的工具类,在开发中通过它们可更直接方便地实现功能。下面将介绍demo中各个工具类的使用,而具体的实现请下载demo查看代码。


因为工作需要,简单研究了一下Android6.0权限申请,在Google提供的sample的基础上,写了一个简单的demo。算是自己的笔记吧,可能会比较混乱,主要是方便以后查看。后期有别的问题,随时更新~

介绍

  • 本demo
    github下载地址!!!

  • Google提供的demo的下载地址

  • 6.0权限的基本知识,以下是需要单独申请的权限,共分为9组,每组只要有一个权限申请成功了,就默认整组权限都可以使用了。

      group:android.permission-group.CONTACTS
        permission:android.permission.WRITE_CONTACTS
        permission:android.permission.GET_ACCOUNTS    
        permission:android.permission.READ_CONTACTS
    
      group:android.permission-group.PHONE
        permission:android.permission.READ_CALL_LOG
        permission:android.permission.READ_PHONE_STATE 
        permission:android.permission.CALL_PHONE
        permission:android.permission.WRITE_CALL_LOG
        permission:android.permission.USE_SIP
        permission:android.permission.PROCESS_OUTGOING_CALLS
        permission:com.android.voicemail.permission.ADD_VOICEMAIL
    
      group:android.permission-group.CALENDAR
        permission:android.permission.READ_CALENDAR
        permission:android.permission.WRITE_CALENDAR
    
      group:android.permission-group.CAMERA
        permission:android.permission.CAMERA
    
      group:android.permission-group.SENSORS
        permission:android.permission.BODY_SENSORS
    
      group:android.permission-group.LOCATION
        permission:android.permission.ACCESS_FINE_LOCATION
        permission:android.permission.ACCESS_COARSE_LOCATION
    
      group:android.permission-group.STORAGE
        permission:android.permission.READ_EXTERNAL_STORAGE
        permission:android.permission.WRITE_EXTERNAL_STORAGE
    
      group:android.permission-group.MICROPHONE
        permission:android.permission.RECORD_AUDIO
    
      group:android.permission-group.SMS
        permission:android.permission.READ_SMS
        permission:android.permission.RECEIVE_WAP_PUSH
        permission:android.permission.RECEIVE_MMS
        permission:android.permission.RECEIVE_SMS
        permission:android.permission.SEND_SMS
        permission:android.permission.READ_CELL_BROADCASTS
    
  • 以下是普通权限,只需要在AndroidManifest.xml中申请即可。

      android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
      android.permission.ACCESS_NETWORK_STATE
      android.permission.ACCESS_NOTIFICATION_POLICY
      android.permission.ACCESS_WIFI_STATE
      android.permission.ACCESS_WIMAX_STATE
      android.permission.BLUETOOTH
      android.permission.BLUETOOTH_ADMIN
      android.permission.BROADCAST_STICKY
      android.permission.CHANGE_NETWORK_STATE
      android.permission.CHANGE_WIFI_MULTICAST_STATE
      android.permission.CHANGE_WIFI_STATE
      android.permission.CHANGE_WIMAX_STATE
      android.permission.DISABLE_KEYGUARD
      android.permission.EXPAND_STATUS_BAR
      android.permission.FLASHLIGHT
      android.permission.GET_ACCOUNTS
      android.permission.GET_PACKAGE_SIZE
      android.permission.INTERNET
      android.permission.KILL_BACKGROUND_PROCESSES
      android.permission.MODIFY_AUDIO_SETTINGS
      android.permission.NFC
      android.permission.READ_SYNC_SETTINGS
      android.permission.READ_SYNC_STATS
      android.permission.RECEIVE_BOOT_COMPLETED
      android.permission.REORDER_TASKS
      android.permission.REQUEST_INSTALL_PACKAGES
      android.permission.SET_TIME_ZONE
      android.permission.SET_WALLPAPER
      android.permission.SET_WALLPAPER_HINTS
      android.permission.SUBSCRIBED_FEEDS_READ
      android.permission.TRANSMIT_IR
      android.permission.USE_FINGERPRINT
      android.permission.VIBRATE
      android.permission.WAKE_LOCK
      android.permission.WRITE_SYNC_SETTINGS
      com.android.alarm.permission.SET_ALARM
      com.android.launcher.permission.INSTALL_SHORTCUT
      com.android.launcher.permission.UNINSTALL_SHORTCUT
    

1. Log打印

demo中提供了LogUtil工具类(出自ZhaoKaiQiang),和普通的Log一样包含Verbose、Debug、Info、Warn、Error、Assert几个等级,还支持将json或xml文本格式化后输出。
使用:
1)控制是否打印。正式打包上线时可设为false:

LogUtil.init(true);

2)打印

//比如打印Error级日志
LogUtil.e(TAG, message);

//将jsonString格式化后打印出来
LogUtil.json(TAG, jsonString);

//将xmlString格式化后打印出来
LogUtil.xml(TAG, xmlString);

这个Log工具类一个很大的好处是,你可以快速地跟踪到调用打印的具体位置。如图:

图片 1

Log跟踪1

点击MoviePresenter即可定位到输出打印的位置

图片 2

Log跟踪2

申请步骤

    1. 将targetSdkVersion设置为23,注意,如果你将targetSdkVersion设置为>=23,则必须按照Android谷歌的要求,动态的申请权限,如果你暂时不打算支持动态权限申请,则targetSdkVersion最大只能设置为22.
  • 2
    在AndroidManifest.xml中申请你需要的权限,包括普通权限和需要申请的特殊权限。

  • 3.开始申请权限,此处分为3部。

  • (1)检查是否由此权限checkSelfPermission(),如果已经开启,则直接做你想做的。

  • (2)如果未开启,则判断是否需要向用户解释为何申请权限shouldShowRequestPermissionRationale。

  • (3)如果需要(即返回true),则可以弹出对话框提示用户申请权限原因,用户确认后申请权限requestPermissions(),如果不需要(即返回false),则直接申请权限requestPermissions()。
    (这里是一部门代码,底部有比较完善的代码,整个demo可以在github中下载)。

图片 3

单个权限申请.png

     /**
         * Requests permission.
         *
         * @param activity
         * @param requestCode request code, e.g. if you need request CAMERA permission,parameters is PermissionUtils.CODE_CAMERA
         */
        public static void requestPermission(final Activity activity, final int requestCode, PermissionGrant permissionGrant) {
            if (activity == null) {
                return;
            }

            Log.i(TAG, "requestPermission requestCode:" + requestCode);
            if (requestCode < 0 || requestCode >= requestPermissions.length) {
                Log.w(TAG, "requestPermission illegal requestCode:" + requestCode);
                return;
            }

            final String requestPermission = requestPermissions[requestCode];

            //如果是6.0以下的手机,ActivityCompat.checkSelfPermission()会始终等于PERMISSION_GRANTED,
        // 但是,如果用户关闭了你申请的权限(如下图,在安装的时候,将一些权限关闭了),ActivityCompat.checkSelfPermission()则可能会导致程序崩溃(java.lang.RuntimeException: Unknown exception code: 1 msg null),
        // 你可以使用try{}catch(){},处理异常,也可以判断系统版本,低于23就不申请权限,直接做你想做的。permissionGrant.onPermissionGranted(requestCode);
//        if (Build.VERSION.SDK_INT < 23) {
//            permissionGrant.onPermissionGranted(requestCode);
//            return;
//        }

            int checkSelfPermission;
            try {
                checkSelfPermission = ActivityCompat.checkSelfPermission(activity, requestPermission);
            } catch (RuntimeException e) {
                Toast.makeText(activity, "please open this permission", Toast.LENGTH_SHORT)
                        .show();
                Log.e(TAG, "RuntimeException:" + e.getMessage());
                return;
            }

            if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "ActivityCompat.checkSelfPermission != PackageManager.PERMISSION_GRANTED");


                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, requestPermission)) {
                    Log.i(TAG, "requestPermission shouldShowRequestPermissionRationale");
                    shouldShowRationale(activity, requestCode, requestPermission);

                } else {
                    Log.d(TAG, "requestCameraPermission else");
                    ActivityCompat.requestPermissions(activity, new String[]{requestPermission}, requestCode);
                }

            } else {
                Log.d(TAG, "ActivityCompat.checkSelfPermission ==== PackageManager.PERMISSION_GRANTED");
                Toast.makeText(activity, "opened:" + requestPermissions[requestCode], Toast.LENGTH_SHORT).show();
//得到权限的时候,就可以在回调里面做你想做的事情了
                permissionGrant.onPermissionGranted(requestCode);
            }
        }

图片 4

6.0以下系统的应用程序安装界面.png

2. 崩溃打印

demo中提供了CrashLogUtil工具类,用于在程序崩溃时,输出异常日志到手机本地文件中,方便在没连接电脑时查看异常信息。
使用方法:
1.Application中初始化

CrashLogUtil.getInstance().init(this);//初始化崩溃打印

2.可指定异常信息所输出的文件(位置)

File dirTemp = FileUtil.generateDirectory(FileUtil.getExternalCacheDir(), "temp");
File fileOutput = FileUtil.generateFile(dirTemp, fileName);
//指定输出文件
CrashLogUtil.getInstance().setFileOutput(fileOutput);

不指定的话,文件默认就保存在/storage/emulated/0/Android/data/com.xxx.xxx/cache/temp下。
程序崩溃时,即可到对应的位置下查看异常信息,如图:

图片 5

查看崩溃日志1

图片 6

查看崩溃日志2

备注!!!

(1)checkSelfPermission:检查是否拥有这个权限
(2)requestPermissions:请求权限,一般会弹出一个系统对话框,询问用户是否开启这个权限。
(3)shouldShowRequestPermissionRationale:Android原生系统中,如果第二次弹出权限申请的对话框,会出现“以后不再弹出”的提示框,如果用户勾选了,你再申请权限,则shouldShowRequestPermissionRationale返回true,意思是说要给用户一个
解释,告诉用户为什么要这个权限。然而,在实际开发中,需要注意的是,很多手机对原生系统做了修改,比如小米,小米4的6.0的shouldShowRequestPermissionRationale
就一直返回false,而且在申请权限时,如果用户选择了拒绝,则不会再弹出对话框了。。。。
所以说这个地方有坑,我的解决方法是,在回调里面处理,如果用户拒绝了这个权限,则打开本应用信息界面,由用户自己手动开启这个权限。
(4)每个应用都有自己的权限管理界面,里面有本应用申请的权限以及各种状态,即使用户已经同意了你申请的权限,他也随时可以关闭

图片 7

权限管理界面.png

3. 手机系统类型

demo中提供了SystemTypeUtil工具类,用于处理与手机系统类型相关的事件。
部分手机的系统是基于原生Android系统改造的(如小米,魅族),所以在有些功能的实现上需要做兼容处理,如设置状态栏中图文的颜色模式、跳转到权限管理页面。

  • 判断手机类型

boolean flag;
flag = SystemTypeUtil.isEMUI();//是否为华为手机
flag = SystemTypeUtil.isMIUI();//是否为小米手机
flag = SystemTypeUtil.isFlyme();//是否为魅族手机
  • 跳转到权限管理页面

//跳转,兼容不同手机系统类型
SystemTypeUtil.goToPermissionManager(context);
  • 设置状态栏中图文的颜色模式(深色模式或亮色模式)

boolean isDark = true;//true表示深色模式,false表示亮色模式
Window window = getWindow();
boolean flag = SystemTypeUtil.setStatusBarLightMode(window, isDark);//返回true表示设置成功

深色模式效果图:

图片 8

深色模式

亮色模式效果图:

图片 9

亮色模式

一次申请多个权限

其实和申请一个权限是一样的,只是requestPermissions(final @NonNull
Activity activity,
final @NonNull String[] permissions, final int
requestCode),里面的permissions给的参数多些而已。

图片 10

申请多个权限.png

 /**
     * 一次申请多个权限
     */
    public static void requestMultiPermissions(final Activity activity, PermissionGrant grant) {

        final List<String> permissionsList = getNoGrantedPermission(activity, false);
        final List<String> shouldRationalePermissionsList = getNoGrantedPermission(activity, true);

        //TODO checkSelfPermission
        if (permissionsList == null || shouldRationalePermissionsList == null) {
            return;
        }
        Log.d(TAG, "requestMultiPermissions permissionsList:" + permissionsList.size() + ",shouldRationalePermissionsList:" + shouldRationalePermissionsList.size());

        if (permissionsList.size() > 0) {
            ActivityCompat.requestPermissions(activity, permissionsList.toArray(new String[permissionsList.size()]),
                    CODE_MULTI_PERMISSION);
            Log.d(TAG, "showMessageOKCancel requestPermissions");

        } else if (shouldRationalePermissionsList.size() > 0) {
            showMessageOKCancel(activity, "should open those permission",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(activity, shouldRationalePermissionsList.toArray(new String[shouldRationalePermissionsList.size()]),
                                    CODE_MULTI_PERMISSION);
                            Log.d(TAG, "showMessageOKCancel requestPermissions");
                        }
                    });
        } else {
            grant.onPermissionGranted(CODE_MULTI_PERMISSION);
        }

    }
  • 关于权限请求结果的回调。Activity实现ActivityCompat.OnRequestPermissionsResultCallback接口,重写onRequestPermissionsResult方法。

     @Override
      public void onRequestPermissionsResult(final int requestCode, @NonNull String[] permissions,
                                             @NonNull int[] grantResults) {
          PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant);
    
      }
    

4. android 6.0权限申请

android6.0后(targetSdkVersion>=23)对于敏感权限,app需要向用户提出授权申请。
demo中提供了一个比较简单PermissionUtil工具类,用于6.0版本的权限申请。github上有不少高star的6.0权限开源库,大家可以自行查阅。

这里说下一般的申请流程:
1)在 AndroidManifest.xml 添加权限声明。
2)使用 checkSelfPermission 检查某个权限是否已经申请。
3)权限未申请,使用 requestPermissions
申请权限,然后会回调onRequestPermissionsResult。
4)在 onRequestPermissionsResult 回调中判断权限是否申请成功。
5)如果申请失败,则使用 shouldShowRequestPermissionRationale
判断用户是否勾选了 “不再提醒”。勾选了的话,则弹出一个 Dialog
引导用户到设置界面授予权限。没勾选的话,可以什么都不做,也可以弹出弹出一个
Dialog 引导用户到设置界面授予权限。

整个申请权限工具类代码

package com.example.android.system.runtimepermissions;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qianxiaoai on 2016/7/7.
 */
public class PermissionUtils {

    private static final String TAG = PermissionUtils.class.getSimpleName();
    public static final int CODE_RECORD_AUDIO = 0;
    public static final int CODE_GET_ACCOUNTS = 1;
    public static final int CODE_READ_PHONE_STATE = 2;
    public static final int CODE_CALL_PHONE = 3;
    public static final int CODE_CAMERA = 4;
    public static final int CODE_ACCESS_FINE_LOCATION = 5;
    public static final int CODE_ACCESS_COARSE_LOCATION = 6;
    public static final int CODE_READ_EXTERNAL_STORAGE = 7;
    public static final int CODE_WRITE_EXTERNAL_STORAGE = 8;
    public static final int CODE_MULTI_PERMISSION = 100;

    public static final String PERMISSION_RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
    public static final String PERMISSION_GET_ACCOUNTS = Manifest.permission.GET_ACCOUNTS;
    public static final String PERMISSION_READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
    public static final String PERMISSION_CALL_PHONE = Manifest.permission.CALL_PHONE;
    public static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
    public static final String PERMISSION_ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    public static final String PERMISSION_ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    public static final String PERMISSION_READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;
    public static final String PERMISSION_WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;

    private static final String[] requestPermissions = {
            PERMISSION_RECORD_AUDIO,
            PERMISSION_GET_ACCOUNTS,
            PERMISSION_READ_PHONE_STATE,
            PERMISSION_CALL_PHONE,
            PERMISSION_CAMERA,
            PERMISSION_ACCESS_FINE_LOCATION,
            PERMISSION_ACCESS_COARSE_LOCATION,
            PERMISSION_READ_EXTERNAL_STORAGE,
            PERMISSION_WRITE_EXTERNAL_STORAGE
    };

    interface PermissionGrant {
        void onPermissionGranted(int requestCode);
    }

    /**
     * Requests permission.
     *
     * @param activity
     * @param requestCode request code, e.g. if you need request CAMERA permission,parameters is PermissionUtils.CODE_CAMERA
     */
    public static void requestPermission(final Activity activity, final int requestCode, PermissionGrant permissionGrant) {
        if (activity == null) {
            return;
        }

        Log.i(TAG, "requestPermission requestCode:" + requestCode);
        if (requestCode < 0 || requestCode >= requestPermissions.length) {
            Log.w(TAG, "requestPermission illegal requestCode:" + requestCode);
            return;
        }

        final String requestPermission = requestPermissions[requestCode];

        //如果是6.0以下的手机,ActivityCompat.checkSelfPermission()会始终等于PERMISSION_GRANTED,
        // 但是,如果用户关闭了你申请的权限,ActivityCompat.checkSelfPermission(),会导致程序崩溃(java.lang.RuntimeException: Unknown exception code: 1 msg null),
        // 你可以使用try{}catch(){},处理异常,也可以在这个地方,低于23就什么都不做,
        // 个人建议try{}catch(){}单独处理,提示用户开启权限。
//        if (Build.VERSION.SDK_INT < 23) {
//            return;
//        }

        int checkSelfPermission;
        try {
            checkSelfPermission = ActivityCompat.checkSelfPermission(activity, requestPermission);
        } catch (RuntimeException e) {
            Toast.makeText(activity, "please open this permission", Toast.LENGTH_SHORT)
                    .show();
            Log.e(TAG, "RuntimeException:" + e.getMessage());
            return;
        }

        if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "ActivityCompat.checkSelfPermission != PackageManager.PERMISSION_GRANTED");


            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, requestPermission)) {
                Log.i(TAG, "requestPermission shouldShowRequestPermissionRationale");
                shouldShowRationale(activity, requestCode, requestPermission);

            } else {
                Log.d(TAG, "requestCameraPermission else");
                ActivityCompat.requestPermissions(activity, new String[]{requestPermission}, requestCode);
            }

        } else {
            Log.d(TAG, "ActivityCompat.checkSelfPermission ==== PackageManager.PERMISSION_GRANTED");
            Toast.makeText(activity, "opened:" + requestPermissions[requestCode], Toast.LENGTH_SHORT).show();
            permissionGrant.onPermissionGranted(requestCode);
        }
    }

    private static void requestMultiResult(Activity activity, String[] permissions, int[] grantResults, PermissionGrant permissionGrant) {

        if (activity == null) {
            return;
        }

        //TODO
        Log.d(TAG, "onRequestPermissionsResult permissions length:" + permissions.length);
        Map<String, Integer> perms = new HashMap<>();

        ArrayList<String> notGranted = new ArrayList<>();
        for (int i = 0; i < permissions.length; i++) {
            Log.d(TAG, "permissions: [i]:" + i + ", permissions[i]" + permissions[i] + ",grantResults[i]:" + grantResults[i]);
            perms.put(permissions[i], grantResults[i]);
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                notGranted.add(permissions[i]);
            }
        }

        if (notGranted.size() == 0) {
            Toast.makeText(activity, "all permission success" + notGranted, Toast.LENGTH_SHORT)
                    .show();
            permissionGrant.onPermissionGranted(CODE_MULTI_PERMISSION);
        } else {
            openSettingActivity(activity, "those permission need granted!");
        }

    }


    /**
     * 一次申请多个权限
     */
    public static void requestMultiPermissions(final Activity activity, PermissionGrant grant) {

        final List<String> permissionsList = getNoGrantedPermission(activity, false);
        final List<String> shouldRationalePermissionsList = getNoGrantedPermission(activity, true);

        //TODO checkSelfPermission
        if (permissionsList == null || shouldRationalePermissionsList == null) {
            return;
        }
        Log.d(TAG, "requestMultiPermissions permissionsList:" + permissionsList.size() + ",shouldRationalePermissionsList:" + shouldRationalePermissionsList.size());

        if (permissionsList.size() > 0) {
            ActivityCompat.requestPermissions(activity, permissionsList.toArray(new String[permissionsList.size()]),
                    CODE_MULTI_PERMISSION);
            Log.d(TAG, "showMessageOKCancel requestPermissions");

        } else if (shouldRationalePermissionsList.size() > 0) {
            showMessageOKCancel(activity, "should open those permission",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(activity, shouldRationalePermissionsList.toArray(new String[shouldRationalePermissionsList.size()]),
                                    CODE_MULTI_PERMISSION);
                            Log.d(TAG, "showMessageOKCancel requestPermissions");
                        }
                    });
        } else {
            grant.onPermissionGranted(CODE_MULTI_PERMISSION);
        }

    }


    private static void shouldShowRationale(final Activity activity, final int requestCode, final String requestPermission) {
        //TODO
        String[] permissionsHint = activity.getResources().getStringArray(R.array.permissions);
        showMessageOKCancel(activity, "Rationale: " + permissionsHint[requestCode], new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCompat.requestPermissions(activity,
                        new String[]{requestPermission},
                        requestCode);
                Log.d(TAG, "showMessageOKCancel requestPermissions:" + requestPermission);
            }
        });
    }

    private static void showMessageOKCancel(final Activity context, String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();

    }

    /**
     * @param activity
     * @param requestCode  Need consistent with requestPermission
     * @param permissions
     * @param grantResults
     */
    public static void requestPermissionsResult(final Activity activity, final int requestCode, @NonNull String[] permissions,
                                                @NonNull int[] grantResults, PermissionGrant permissionGrant) {

        if (activity == null) {
            return;
        }
        Log.d(TAG, "requestPermissionsResult requestCode:" + requestCode);

        if (requestCode == CODE_MULTI_PERMISSION) {
            requestMultiResult(activity, permissions, grantResults, permissionGrant);
            return;
        }

        if (requestCode < 0 || requestCode >= requestPermissions.length) {
            Log.w(TAG, "requestPermissionsResult illegal requestCode:" + requestCode);
            Toast.makeText(activity, "illegal requestCode:" + requestCode, Toast.LENGTH_SHORT).show();
            return;
        }

        Log.i(TAG, "onRequestPermissionsResult requestCode:" + requestCode + ",permissions:" + permissions.toString()
                + ",grantResults:" + grantResults.toString() + ",length:" + grantResults.length);

        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "onRequestPermissionsResult PERMISSION_GRANTED");
            //TODO success, do something, can use callback
            permissionGrant.onPermissionGranted(requestCode);

        } else {
            //TODO hint user this permission function
            Log.i(TAG, "onRequestPermissionsResult PERMISSION NOT GRANTED");
            //TODO
            String[] permissionsHint = activity.getResources().getStringArray(R.array.permissions);
            openSettingActivity(activity,  "Result" + permissionsHint[requestCode]);
        }

    }

    private static void openSettingActivity(final Activity activity, String message) {

        showMessageOKCancel(activity, message, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Log.d(TAG, "getPackageName(): " + activity.getPackageName());
                Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                intent.setData(uri);
                activity.startActivity(intent);
            }
        });
    }


    /**
     * @param activity
     * @param isShouldRationale true: return no granted and shouldShowRequestPermissionRationale permissions, false:return no granted and !shouldShowRequestPermissionRationale
     * @return
     */
    public static ArrayList<String> getNoGrantedPermission(Activity activity, boolean isShouldRationale) {

        ArrayList<String> permissions = new ArrayList<>();

        for (int i = 0; i < requestPermissions.length; i++) {
            String requestPermission = requestPermissions[i];


            //TODO checkSelfPermission
            int checkSelfPermission = -1;
            try {
                checkSelfPermission = ActivityCompat.checkSelfPermission(activity, requestPermission);
            } catch (RuntimeException e) {
                Toast.makeText(activity, "please open those permission", Toast.LENGTH_SHORT)
                        .show();
                Log.e(TAG, "RuntimeException:" + e.getMessage());
                return null;
            }

            if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "getNoGrantedPermission ActivityCompat.checkSelfPermission != PackageManager.PERMISSION_GRANTED:" + requestPermission);

                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, requestPermission)) {
                    Log.d(TAG, "shouldShowRequestPermissionRationale if");
                    if (isShouldRationale) {
                        permissions.add(requestPermission);
                    }

                } else {

                    if (!isShouldRationale) {
                        permissions.add(requestPermission);
                    }
                    Log.d(TAG, "shouldShowRequestPermissionRationale else");
                }

            }
        }

        return permissions;
    }

}

5. 网络状态

demo中提供了NetworkUtil工具类,用于查询网络状态

//当前网络是否可用,返回true表示可用,false表示不可以用
NetworkUtil.isNetWorkAvailable(context);

//当前网络是否为wifi网络,返回true表示是,false表示不是
NetworkUtil.isWifiConnected(context);

//当前网络是否为手机移动网络,返回true表示是,false表示不是
NetworkUtil.isMobileConnected(context);

...

界面调用代码

package com.example.android.system.runtimepermissions;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Toast;

import com.example.android.common.logger.Log;

/**
 * Created by qianxiaoai on 2016/7/8.
 */
public class PermissionActivity extends FragmentActivity implements ActivityCompat.OnRequestPermissionsResultCallback{
    private static final String TAG = PermissionActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        PermissionsFragment fragment = new PermissionsFragment();
        transaction.replace(R.id.content_fragment, fragment);
        transaction.commit();

    }

    /**
     * Called when the 'show camera' button is clicked.
     * Callback is defined in resource layout definition.
     */
    public void showCamera(View view) {
        Log.i(TAG, "Show camera button pressed. Checking permission.");
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_CAMERA, mPermissionGrant);
    }

    public void getAccounts(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_GET_ACCOUNTS, mPermissionGrant);
    }

    public void callPhone(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_CALL_PHONE, mPermissionGrant);
    }

    public void readPhoneState(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_PHONE_STATE, mPermissionGrant);
    }

    public void accessFineLocation(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_ACCESS_FINE_LOCATION, mPermissionGrant);
    }

    public void accessCoarseLocation(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_ACCESS_COARSE_LOCATION, mPermissionGrant);
    }

    public void readExternalStorage(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_EXTERNAL_STORAGE, mPermissionGrant);
    }

    public void writeExternalStorage(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_WRITE_EXTERNAL_STORAGE, mPermissionGrant);
    }

    public void recordAudio(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_RECORD_AUDIO, mPermissionGrant);
    }


    private PermissionUtils.PermissionGrant mPermissionGrant = new PermissionUtils.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            switch (requestCode) {
                case PermissionUtils.CODE_RECORD_AUDIO:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_RECORD_AUDIO", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_GET_ACCOUNTS:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_GET_ACCOUNTS", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_READ_PHONE_STATE:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_READ_PHONE_STATE", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_CALL_PHONE:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_CALL_PHONE", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_CAMERA:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_CAMERA", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_ACCESS_FINE_LOCATION:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_ACCESS_FINE_LOCATION", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_ACCESS_COARSE_LOCATION:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_ACCESS_COARSE_LOCATION", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_READ_EXTERNAL_STORAGE:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_READ_EXTERNAL_STORAGE", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_WRITE_EXTERNAL_STORAGE:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_WRITE_EXTERNAL_STORAGE", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * Callback received when a permissions request has been completed.
     */
    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant);
    }
}

6. File相关

demo中提供了FileUtil工具类,用于处理File相关的操作

  • SD卡是否能用

FileUtil.isSDCardAvailable();
  • 创建文件夹

/**
* 创建一个文件夹, 存在则返回, 不存在则新建
*
* @param String parentDirectory 父目录路径
* @param String directory  目录名
* @return 文件,null代表失败
*/
FileUtil.generateDirectory(parentDirectory, directory);

/**
 * 创建一个文件夹, 存在则返回, 不存在则新建
 *
 * @param File parentDirectory 父目录
 * @param String directory  目录名
 * @return 文件,null代表失败
 */
 FileUtil.generateDirectory(parentDirectory, directory)
  • 创建文件

/**
 * 创建一个文件, 存在则返回, 不存在则新建
 *
 * @param File catalog 父目录
 * @param String name    文件名
 * @return 文件,null代表失败
 */
 FileUtil.generateFile(catalog, name);

/**
* 根据全路径创建一个文件
*
* @param filePath 文件全路径
* @return 文件,null代表失败
*/
FileUtil.generateFile(filePath);
  • 删除文件

/**
* 删除文件/文件夹
* 如果是文件夹,则会删除其下的文件以及它本身
* @param file file
* @return true代表成功删除
*/
FileUtil.deleteFile(file);
  • 计算文件大小

/**
 * 计算文件/文件夹的大小
 * @param file 文件或文件夹
 * @return long 文件大小
 */
FileUtil.calculateFileSize(file);
  • 获取系统提供的文件夹路径

//返回"/data"目录
FileUtil.getDataDirectory();

//返回"/storage/emulated/0"目录
FileUtil.getExternalStorageDirectory();

//返回"/system"目录
FileUtil.getRootDirectory();

//返回"/cache"目录
FileUtil.getDownloadCacheDirectory();

//返回"/data/user/0/com.xxx.xxx/cache"目录
FileUtil.getCacheDir();

//返回"/data/user/0/com.xxx.xxx/files"目录
FileUtil.getFilesDir();

//返回"/storage/emulated/0/Android/data/com.xxx.xxx/cache"目录
FileUtil.getExternalCacheDir();

/**
 * @param String type 所放的文件的类型,传入的参数是Environment类中的DIRECTORY_XXX静态变量
 * @return 返回"/storage/emulated/0/xxx"目录
 *         例如传入Environment.DIRECTORY_ALARMS则返回"/storage/emulated/0/Alarms"
 */
FileUtil.getExternalStoragePublicDirectory(type);

/**
 * @param String type 所放的文件的类型,传入的参数是Environment类中的DIRECTORY_XXX静态变量
 * @return  返回"/storage/emulated/0/Android/data/com.xxx.xxx/files/Alarms"目录
 *          例如传入Environment.DIRECTORY_ALARMS则返回"/storage/emulated/0/Android/data/com.xxx.xxx/files/Alarms"
 */
FileUtil.getExternalFilesDir(type);

xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingLeft="@dimen/horizontal_page_margin"
              android:paddingRight="@dimen/horizontal_page_margin"
              android:paddingTop="@dimen/vertical_page_margin"
              android:paddingBottom="@dimen/vertical_page_margin"
              android:orientation="vertical"
    >

    <FrameLayout
        android:id="@+id/content_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Camera"
                    android:id="@+id/button_camera"
                    android:onClick="showCamera"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="RECORD_AUDIO"
                    android:onClick="recordAudio"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="GET_ACCOUNTS"
                    android:onClick="getAccounts"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="CALL_PHONE"
                    android:onClick="callPhone"/>
            </LinearLayout>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="PERMISSION_READ_PHONE_STATE"
                android:onClick="readPhoneState"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ACCESS_FINE_LOCATION"
                android:onClick="accessFineLocation"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ACCESS_COARSE_LOCATION"
                android:onClick="accessCoarseLocation"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="READ_EXTERNAL_STORAGE"
                android:onClick="readExternalStorage"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="WRITE_EXTERNAL_STORAGE"
                android:onClick="writeExternalStorage"/>

        </LinearLayout>
    </ScrollView>

</LinearLayout>

7. Activity栈管理

demo中提供了ActivityStackManager工具类,用于存放管理Activity栈。
使用流程:
1.在activity创建时调用pushOneActivity(Activity
activity)推入栈,可放于Activity基类中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mStackManager = ActivityStackManager.getInstance();
    mStackManager.pushOneActivity(this);
}

2.在activity销毁时调用popOneActivity(Activity
activity)弹出栈,可放于Activity基类中

@Override
protected void onDestroy() {
    mStackManager.popOneActivity(this);
    super.onDestroy();
}

3.当你想要退出某个Activity时,可调用exitActivity(Class cls)

mStackManager.exitActivity(CollectActivity.class);

4.当你想要退出整个应用(所有Activity)时,可调用exitApplication()

mStackManager.exitApplication();

还提供了其他方法,具体请查看demo代码

清单文件申请的权限

  <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>

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

8. SharedPreferences

demo中提供了PreferenceUtil工具类,用于SharedPreferences的数据插入、读取等操作。

  • 获取SharedPreferences

SharedPreferences sp = PreferenceUtil.getPreference(context, spName);
  • 插入数据

//int类型
PreferenceUtil.putInt(sp, key, value);
//String类型
PreferenceUtil.putString(sp, key, value);
//boolean类型
PreferenceUtil.putBoolean(sp, key, value);
//float类型
PreferenceUtil.putFloat(sp, key, value);
//long类型
PreferenceUtil.putLong(sp, key, value);
  • 读取数据

//int类型
PreferenceUtil.getInt(sp, key, defaultValue);
//String类型
PreferenceUtil.getString(sp, key, defaultValue);
//boolean类型
PreferenceUtil.getBoolean(sp, key, defaultValue);
//float类型
PreferenceUtil.getFloat(sp, key, defaultValue);
//long类型
PreferenceUtil.getLong(sp, key, defaultValue);

//读取所有数据,返回Map
PreferenceUtil.getAll(sp);
  • 清空数据

PreferenceUtil.clearAll(sp);

部分 资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="permissions">
        <item>@string/permission_recode_audio_hint</item>
        <item>@string/permission_get_accounts_hint</item>
        <item>@string/permission_read_phone_hint</item>
        <item>@string/permission_call_phone_hint</item>
        <item>@string/permission_camera_hint</item>
        <item>@string/permission_access_fine_location_hint</item>
        <item>@string/permission_access_coarse_location_hint</item>
        <item>@string/permission_read_external_hint</item>
        <item>@string/permission_white_external_hint</item>
    </string-array>
</resources>

 <string name="permission_get_accounts_hint">没有此权限,无法开启这个功能,请开启权限。PERMISSION_GET_ACCOUNTS</string>
    <string name="permission_read_phone_hint">没有此权限,无法开启这个功能,请开启权限。PERMISSION_READ_PHONE_STATE</string>
    <string name="permission_call_phone_hint">没有此权限,无法开启这个功能,请开启权限。PERMISSION_CALL_PHONE</string>
    <string name="permission_camera_hint">没有此权限,无法开启这个功能,请开启权限。PERMISSION_CAMERA</string>
    <string name="permission_access_fine_location_hint">没有此权限,无法开启这个功能,请开启权限。PERMISSION_ACCESS_FINE_LOCATION</string>
    <string name="permission_access_coarse_location_hint">没有此权限,无法开启这个功能,请开启权限。PERMISSION_ACCESS_COARSE_LOCATION</string>
    <string name="permission_read_external_hint">没有此权限,无法开启这个功能,请开启权限。PERMISSION_READ_EXTERNAL_STORAGE</string>
    <string name="permission_white_external_hint">没有此权限,无法开启这个功能,请开启权限。PERMISSION_WRITE_EXTERNAL_STORAGE</string>
    <string name="permission_recode_audio_hint">没有此权限,无法开启这个功能,请开启权限。PERMISSION_RECORD_AUDIO</string>

9. Toast

demo中提供了ToastUtil工具类,方便吐司。
使用流程
1.Application中初始化

ToastUtil.init(this);//初始化吐司

2.在要吐司的地方调用show

ToastUtil.show("hello world");

ToastUtil.show(R.string.appName);

10. 密度单位转换

demo提供了DensityUtil工具类,用于密度单位的转换

  • px转dp

int dp = DensityUtil.px2dp(context, px);
  • dp转px

int px = DensityUtil.dp2px(context, dp);
  • sp转px

int px = DensityUtil.sp2px(context, sp);
  • px转sp

int sp = DensityUtil.px2sp(context, px);

11. 高斯模糊

demo中提供了BlurUtil工具类,用于对Bitmap做高斯模糊处理,可以选用Java原生方式和RenderScript方式。

  • Java原生方式

//使用Java实现的高斯模糊效果(性能较低,模糊半径0-100,越大越模糊)
Bitmap bitmapAfter = BlurUtil.javaBlur(bitmapBefore, 15, true);
  • RenderScript方式

/**
* 使用RenderScript实现的高斯模糊效果(性能较高,模糊半径0-25,越大越模糊)
* 需在module下的build.gradle中加入
* renderscriptTargetApi 19
* renderscriptSupportModeEnabled true
*/
Bitmap bitmapAfter = BlurUtil.rsBlur(MyApplication.getInstance(), bitmapBefore, 15);

图片 11

高斯模糊效果

12. 加密

demo中提供了HashCoderUtil工具类,用于md5加密和sha1加密。

  • md5加密

String encrpt = HashCoderUtil.md5Crypt(password.getBytes());
//加盐值
String encrpt = HashCoderUtil.md5Crypt(password.getBytes(),salt);
  • sha1加密

String encrpt = HashCoderUtil.sha1Crypt(password.getBytes());
//加盐值
String encrpt = HashCoderUtil.sha1Crypt(password.getBytes(),salt);

13. 其他

demo中还提供了一个CommonUtil,负责一些比较杂碎的功能,例如

  • 获取设备唯一ID号

String deviceId = CommonUtil.getDeviceUniqueId(context);
  • 创建/删除桌面快捷方式

//创建
CommonUtil.creatShortcut(context);
//删除
CommonUtil.delShortcut(context);
  • 扩展View的触摸范围

//扩大view上下左右10个单位触摸的范围
CommonUtil.expandViewTouchDelegate(view,10,10,10,10);

另外,BlankJ提供了一个各种工具类的开源项目,大家各取所需吧。
https://github.com/Blankj/AndroidUtilCode/blob/master/README-CN.md


详细代码请看demo,地址https://github.com/LJYcoder/DevBase
demo的内容流程,请看《安卓开发框架(MVP+主流框架+基类+工具类)—
开篇》