说明
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
package com.xiets.demoapp;
import android.Manifest; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; 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.view.View; import android.widget.Toast;
/** * 一键备份通讯录 * * @author xietansheng */ public class MainActivity extends AppCompatActivity {
private static final int MY_PERMISSION_REQUEST_CODE = 10000;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
/** * 点击按钮,将通讯录备份保存到外部存储器备。 * * 需要3个权限(都是危险权限): * 1. 读取通讯录权限; * 2. 读取外部存储器权限; * 3. 写入外部存储器权限. */ public void click(View view) { /** * 第 1 步: 检查是否有相应的权限,根据自己需求,进行添加相应的权限 */ boolean isAllGranted = checkPermissionAllGranted( new String[] { Manifest.permission.READ_CONTACTS, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE } ); // 如果这3个权限全都拥有, 则直接执行备份代码 if (isAllGranted) { doBackup(); return; }
/** * 第 2 步: 请求权限 */ // 一次请求多个权限, 如果其他有权限是已经授予的将会自动忽略掉 ActivityCompat.requestPermissions( this, new String[] { Manifest.permission.READ_CONTACTS, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }, MY_PERMISSION_REQUEST_CODE ); }
/** * 检查是否拥有指定的所有权限 */ private boolean checkPermissionAllGranted(String[] permissions) { for (String permission : permissions) { if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { // 只要有一个权限没有被授予, 则直接返回 false return false; } } return true; }
/** * 第 3 步: 申请权限结果返回处理 */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_PERMISSION_REQUEST_CODE) { boolean isAllGranted = true;
// 判断是否所有的权限都已经授予了 for (int grant : grantResults) { if (grant != PackageManager.PERMISSION_GRANTED) { isAllGranted = false; break; } }
if (isAllGranted) { // 如果所有的权限都授予了, 则执行备份代码 doBackup();
} else { // 弹出对话框告诉用户需要权限的原因, 并引导用户去应用权限管理中手动打开权限按钮 openAppDetails(); } } }
/** * 第 4 步: 备份通讯录操作 */ private void doBackup() { // 本文主旨是讲解如果动态申请权限, 具体备份代码不再展示, 就假装备份一下 Toast.makeText(this, "正在备份通讯录...", Toast.LENGTH_SHORT).show(); }
/** * 打开 APP 的详情设置 */ private void openAppDetails() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("备份通讯录需要访问 “通讯录” 和 “外部存储器”,请到 “应用信息 -> 权限” 中授予!"); builder.setPositiveButton("去手动授权", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setData(Uri.parse("package:" + getPackageName())); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); startActivity(intent); } }); builder.setNegativeButton("取消", null); builder.show(); }
} |