android简单学习6.0的动态权限申请

推荐阅读

android6.0以后一部分危险权限需要动态申请权限,

下面通过一个打电话demo来演示(源于上述推荐阅读)。

MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.qefee.pj.testpermissionutil;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.qefee.pj.testpermissionutil.epermission.PermissionUtil;

public class MainActivity extends AppCompatActivity {

private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;

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

Button submitButton = (Button) findViewById(R.id.submitButton);

submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
testCall();
}
});
}

public void testCall() {
if (PermissionUtil.hasCallPhonePerm(this)) { // 是否有权限
callPhone();
} else {
if (PermissionUtil.hasCallPhonePermReason(this)) { // 是否显示申请权限的原因(被拒一次后再次申请时候)
Toast.makeText(MainActivity.this, "没有授予打电话权限的话,打不了电话哦!", Toast.LENGTH_SHORT).show();

PermissionUtil.requestCallPhonePerm(this, MY_PERMISSIONS_REQUEST_CALL_PHONE); // 申请权限
} else {
PermissionUtil.requestCallPhonePerm(this, MY_PERMISSIONS_REQUEST_CALL_PHONE);
}
}
}

private void callPhone() {
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + "10086");
intent.setData(data);
startActivity(intent);
}

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

switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CALL_PHONE:
handleCallPermission(grantResults[0]);
break;
default:
Toast.makeText(MainActivity.this, "unknown request code", Toast.LENGTH_SHORT).show();
}
}

private void handleCallPermission(int grantResult) {
if (grantResult == PackageManager.PERMISSION_GRANTED) {
callPhone();
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}

PermissionUtil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.qefee.pj.testpermissionutil.epermission;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
* PermissionUtil.
* <ul>
* <li>date: 16/7/18</li>
* </ul>
*
* @author tongjin
*/

public class PermissionUtil {
/**
* 是否拥有权限.
*
* @param context context
* @param permission permission
* @return 判断结果
*/

public static boolean has(@NonNull Context context, @NonNull String permission) {
return ContextCompat.checkSelfPermission(context, permission)
== PackageManager.PERMISSION_GRANTED;
}

public static boolean hasReason(@NonNull Activity activity,
@NonNull String permission)
{

return ActivityCompat.shouldShowRequestPermissionRationale(activity,
permission);
}

/**
* 申请权限.
*
* @param activity activity
* @param permissions permissions
* @param requestCode requestCode
*/

public static void request(final @NonNull Activity activity,
final @NonNull String[] permissions,
final int requestCode)
{

ActivityCompat.requestPermissions(activity,
permissions,
requestCode);
}

/**
* 申请权限.
*
* @param activity activity
* @param permission permission
* @param requestCode requestCode
*/

public static void request(final @NonNull Activity activity,
final @NonNull String permission,
final int requestCode)
{

String[] permissions = {permission};
request(activity,
permissions,
requestCode);
}

/**
* 是否拥有打电话权限.
*
* @param context context
* @return 判断结果
*/

public static boolean hasCallPhonePerm(@NonNull Context context) {
return has(context, Manifest.permission.CALL_PHONE);
}

/**
* 是否有申请打电话权限的原因.
*
* @param activity activity
* @return 判断结果
*/

public static boolean hasCallPhonePermReason(@NonNull Activity activity) {
return hasReason(activity,
Manifest.permission.CALL_PHONE);
}

/**
* 请求打电话权限.
*
* @param activity activity
* @param requestCode requestCode
*/

public static void requestCallPhonePerm(final @NonNull Activity activity,
final int requestCode)
{

request(activity,
Manifest.permission.CALL_PHONE,
requestCode);
}
}
Fork me on GitHub