# 最近有同学问到6.0动态权限怎么处理,帮他写了个demo,现在把代码分享出来,希望可以帮到有需要的人。#
## 我总结了三个步骤,简称动态申请权限三部曲! ##
## 1,MainActivity.java代码 ##
package com.cui.permissionutils;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.cui.permissionutils.utils.CommonUtil;
import com.cui.permissionutils.utils.PermissionUtil;
import com.cui.permissionutils.utils.PermissionsChecker;
public class MainActivity extends AppCompatActivity {
private Context mContext;
private EditText et_num;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mContext = MainActivity.this;
et_num = (EditText) findViewById(R.id.et_num);
btn = (Button) findViewById(R.id.btn);
//android 6.0权限问题
grantPermission();
btn.setOnClickListener(new MyOnClickListener());
}
class MyOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
/**
* 动态申请权限 第三步
*/
//判断相机权限是否打开
if (PermissionsChecker.checkIsLacksPermission(mContext, Manifest.permission.CALL_PHONE)) {
CommonUtil.sendToast(mContext, "请打开电话权限");
return;
}
String num = et_num.getText().toString().trim();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + num);
intent.setData(data);//uri
//启动Activity
startActivity(intent);
}
}
/**
* 动态申请权限 第一步
*/
public void grantPermission() {
PermissionUtil.requestPermissions(this,
new String[]{
Manifest.permission.CALL_PHONE,
});
}
/**
* 动态申请权限 第二步
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
for (int i = 0; i < permissions.length; i++) {
if (permissions[i] == Manifest.permission.CALL_PHONE) {
if (grantResults[i] != 0) {
CommonUtil.sendToast(mContext, "请打开电话权限");
}
}
}
}
}
主界面布局代码activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<EditText
android:id="@+id/et_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入号码" />
<Button
android:id="@+id/btn"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨打" />
</LinearLayout>
清单文件代码 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cui.permissionutils">
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
# 下面是几个常见的工具类 #
## CommonUtil ##
public class CommonUtil {
/**
* toast提示
* @param mContext
* @param text
*/
public static void sendToast(Context mContext, String text) {
Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
}
}
## PermissionUtil ##
package com.cui.permissionutils.utils;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Admin on 2017/6/26.
*/
public class PermissionUtil {
public static boolean requestPermissions(Activity activity, String[] permissions) {
return requestPermissions(activity, permissions, null, 0);
}
public static boolean requestPermissions(Activity activity, String[] permissions, String requestPermissionRationale) {
return requestPermissions(activity, permissions, requestPermissionRationale, 0);
}
/**
* @param activity
* @param permissions
* @param requestPermissionRationale
* @param requestCode
* @return true 表示成功进入ActivityCompat.requestPermissions()方法
*/
public static boolean requestPermissions(Activity activity, String[] permissions, String requestPermissionRationale, int requestCode) {
//android 6.0引入权限管理功能,当前手机版本为android6.0及以上,请求写入权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<String> list = new ArrayList<>();
for (int i = 0; i < permissions.length; i++) {
int permissionStatus = ContextCompat.checkSelfPermission(activity, permissions[i]);
if (permissionStatus != PackageManager.PERMISSION_GRANTED) {
boolean isShowRationale = ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[i]);
if (isShowRationale && requestPermissionRationale != null)
CommonUtil.sendToast(activity, requestPermissionRationale);
list.add(permissions[i]);
}
}
if (!list.isEmpty()) {
ActivityCompat.requestPermissions(activity, list.toArray(new String[]{}), requestCode);
return true;
}
}
return false;
}
}
## PermissionsChecker ##
package com.cui.permissionutils.utils;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
/**
* Created by Admin on 2017/6/26.
*/
public class PermissionsChecker {
/**
* 检查是否缺少指定权限
* @param mContext
* @param permission 指定权限
* @return
*/
public static boolean checkIsLacksPermission(Context mContext, String permission) {
return ContextCompat.checkSelfPermission(mContext, permission) ==
PackageManager.PERMISSION_DENIED;
}
/**
* 申请指定权限
* @param mActivity
* @param permissions 指定权限
* @param flag
*/
public static void requestPermission(Activity mActivity, String[] permissions, int flag) {
ActivityCompat.requestPermissions(mActivity, permissions, flag);
}
/**
* 检查当前手机系统是否需要动态申请权限
* @return
*/
public static boolean chickIsRequestPermissions() {
boolean flag = false;
/**
* 判断当前系统是否是高于等于6.0
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
/**
* 高于等于6.0 需要动态申请权限
*/
flag = true;
}
return flag;
}
}
最后附上demo链接!http://download.csdn.net/detail/world_kun/9881289
相关博文:http://blog.csdn.net/wwdlss/article/details/52909098
有问题欢迎留言:2322604972@qq.com