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
|
package com.sunjiajia.monkeyandroid.utils;
import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Uri; import android.provider.Settings; import android.support.design.widget.Snackbar; import android.support.v4.app.ActivityCompat; import android.view.View; import com.sunjiajia.monkeyandroid.R; import java.util.ArrayList;
public class PermissionsUtil {
private static final int REQUEST_STATUS_CODE = 0x001; private static final int REQUEST_PERMISSION_SETTING = 0x002;
private static String[] PERMISSIONS_GROUP = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.INTERNET, Manifest.permission.CAMERA, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_CONTACTS };
public static void checkAndRequestPermissions(final Activity activity) {
ArrayList<String> denidArray = new ArrayList<>();
for (String permission : PERMISSIONS_GROUP) { int grantCode = ActivityCompat.checkSelfPermission(activity, permission); if (grantCode == PackageManager.PERMISSION_DENIED) { denidArray.add(permission); } }
String[] denidPermissions = denidArray.toArray(new String[denidArray.size()]);
if (denidPermissions.length > 0) { for (String permission : denidArray) { if (!showRationaleUI(activity, permission)) { if (!isAppFirstRun(activity)) { Snackbar snackbar = Snackbar.make(activity.findViewById(R.id.btn_sdcard_avail), "这次是真的需要去授权了", Snackbar.LENGTH_INDEFINITE); snackbar.setAction("前往设置", new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", activity.getPackageName(), null); intent.setData(uri); activity.startActivityForResult(intent, REQUEST_PERMISSION_SETTING); } }); snackbar.show(); } } break; } requestPermissions(activity, denidPermissions); } }
public static boolean showRationaleUI(Activity activity, String permission) { return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission); }
public static void requestPermissions(Activity activity, String[] permissions) { ActivityCompat.requestPermissions(activity, permissions, REQUEST_STATUS_CODE); }
public static boolean isAppFirstRun(Activity activity) { SharedPreferences sp = activity.getSharedPreferences("config", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit();
if (sp.getBoolean("first_run", true)) { editor.putBoolean("first_run", false); editor.commit(); return true; } else { editor.putBoolean("first_run", false); editor.commit(); return false; } } }
|