public class PermissonActivity extends AppCompatActivity {
private String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
private final int PERMISSIONREQUESTCODE = 1000;
private String TAG = "PermissonActivity";
private SharedPreferences sp;
private SharedPreferences.Editor spEdit;
private static final String PERMISSIONTYPE = "permissionType";
private Gson gson;
ActivityResultLauncher<String[]> stringActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(),
new ActivityResultCallback<Map<String, Boolean>>() {
@Override
public void onActivityResult(Map<String, Boolean> result) {
}
});
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testpermissonlay);
initView();
initSp();
initTest();
checkPermisson();
}
private void strartCammel() {
}
private void initSp() {
gson = new Gson();
sp = getSharedPreferences("testSQ", Context.MODE_PRIVATE);
spEdit = sp.edit();
}
private void initView() {
String camera = Manifest.permission.CAMERA;
String accessFineLocation = Manifest.permission.ACCESS_FINE_LOCATION;
Log.d(TAG, "testSq" + " camera : " + camera + " accessFineLocation: " + accessFineLocation);
Button cameBtn = findViewById(R.id.camebtn);
cameBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onClickcheckPermisson();
}
});
}
private void onClickcheckPermisson() {
boolean isGranted = true;
List<String> noPermissionList = new ArrayList<>();
String[] noPermiss ;
for (int i = 0; i < permissions.length; i++) {
if (checkSelfPermission(permissions[i]) != PackageManager.PERMISSION_GRANTED) {
isGranted = false;
noPermissionList.add(permissions[i]);
}
}
if (isGranted) {
} else {
String[] needPermiss = noPermissionList.toArray(new String[noPermissionList.size()]);
stringActivityResultLauncher.launch(needPermiss);
}
}
private void checkPermisson() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "testSQ :" + " 权限授权了 ");
} else if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
// 点击禁止,授权弹窗没有(注:应用下次再次进入),跳转到设置页面手动设置权限
// 权限被禁止,但是未选择【禁止后不再询问】:shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) = true
boolean b = shouldShowRequestPermissionRationale(Manifest.permission.CAMERA);
Toast.makeText(getApplicationContext(), ": " + b, Toast.LENGTH_LONG).show();
Log.d(TAG, "testSQ :" + " shouldShowRequestPermissionRationale " + b);
SharedPreferences sp = getSharedPreferences("testSQ", Context.MODE_PRIVATE);
String permissonListStr = sp.getString(PERMISSIONTYPE, null);
ArrayList<String> listPetmission = new ArrayList<>();
if (!TextUtils.isEmpty(permissonListStr)) {
listPetmission.clear();
ArrayList<String> listName = new Gson().fromJson(permissonListStr, new TypeToken<List<String>>() {
}.getType());
if (listName != null && listName.size() > 0) {
Toast.makeText(PermissonActivity.this, "有没有授权的权限", Toast.LENGTH_SHORT).show();
String[][] optionPermiss = PermissionConstant.OPTION_PERMISS;
for (int i = 0; i < listName.size(); i++) {
for (int j = 0; j < optionPermiss.length; j++) {
if (optionPermiss[j][0].equals(listName.get(i))) {
listPetmission.add(optionPermiss[j][1]);
}
}
}
// 权限没有通过,-> 弹窗提示没有通过的权限 -> setting设置页面手动
// 提示对应的权限
showSettingDialog(listPetmission);
}
}
} else {
Log.d(TAG, "testSQ :" + " PERMISSION_GRANTED ");
// 没有此权限就继续申请
requestPermissions(permissions, PERMISSIONREQUESTCODE);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean grantPermission = true;
ArrayList<String> noPermissionType = new ArrayList<>();
switch (requestCode) {
case PERMISSIONREQUESTCODE:
if (grantResults != null && grantResults.length > 0) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
grantPermission = false;
noPermissionType.add(permissions[i]);
spEdit.putString(PERMISSIONTYPE, gson.toJson(noPermissionType)).commit();
}
}
if (grantPermission) {
// 权限通过,首页授权通过不需要处理
} else {
// 首页的应用第一次不用求
Toast.makeText(PermissonActivity.this, "没有授予权限", Toast.LENGTH_SHORT).show();
}
}
break;
default:
break;
}
}
private void showSettingDialog(ArrayList<String> permissonNameList) {
Dialog dialog = new Dialog(PermissonActivity.this);
dialog.setContentView(R.layout.testdialayout);
Button bimitBtn = dialog.findViewById(R.id.bimit);
Button cancelBtn = dialog.findViewById(R.id.cancel);
RecyclerView recyclerViewName = dialog.findViewById(R.id.permissionname);
if (recyclerViewName != null) {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PermissonActivity.this);
PermissDialogAdapter permissDialogAdapter = new PermissDialogAdapter(PermissonActivity.this, permissonNameList);
recyclerViewName.setLayoutManager(linearLayoutManager);
recyclerViewName.setAdapter(permissDialogAdapter);
}
bimitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// setting设置页面
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
private void initTest() {
}
}
public class PermissionConstant {
public static final String[][] OPTION_PERMISS = {
{Manifest.permission.ACTIVITY_RECOGNITION, "拍照"},
{Manifest.permission.CAMERA, "拍照"},
{Manifest.permission.READ_CALL_LOG, "拍照"},
{Manifest.permission.ACCEPT_HANDOVER, "拍照"},
{Manifest.permission.ACCESS_BACKGROUND_LOCATION, "拍照"},
{Manifest.permission.ACCESS_BLOBS_ACROSS_USERS, "拍照"},
{Manifest.permission.ACCESS_CHECKIN_PROPERTIES, "访问检入属性"},
{Manifest.permission.ACCESS_COARSE_LOCATION, "大概位置"},
{Manifest.permission.ACCESS_FINE_LOCATION, "精准的(GPS)位置"},
{Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS, "访问额外的位置信息提供程序命令"},
{Manifest.permission.ACCESS_MEDIA_LOCATION, "查看网络状态"},
{Manifest.permission.ACCESS_NETWORK_STATE, "查看网络状态"},
{Manifest.permission.ACCESS_NOTIFICATION_POLICY, "拍照"},
{Manifest.permission.ACCESS_WIFI_STATE, "查看 WLAN 状态"},
{Manifest.permission.ACCOUNT_MANAGER, "作为帐户身份验证程序"},
{Manifest.permission.ACTIVITY_RECOGNITION, "拍照"},
{Manifest.permission.ADD_VOICEMAIL, "拍照"},
{Manifest.permission.ANSWER_PHONE_CALLS, "拍照"},
{Manifest.permission.BIND_ACCESSIBILITY_SERVICE, "拍照"},
{Manifest.permission.BIND_APPWIDGET, "选择窗口小部件"},
{Manifest.permission.BIND_AUTOFILL_SERVICE, "拍照"},
{Manifest.permission.BIND_CALL_REDIRECTION_SERVICE, "拍照"},
{Manifest.permission.BIND_CARRIER_MESSAGING_CLIENT_SERVICE, "拍照"}
};
}
shouldShowRequestPermissionRationale()使用
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Nodejs对内置模块fs的进阶使用方法技巧,querystring使用技巧及进行escape和unescape编...