答案 0 :(得分:1)
对于活动,您应该检查onCreate状态或SplashActivity中的共享首选项,对于片段,您应该将sharedPreference状态传递给mainactivity类。
首先检查sharedPreference状态并创建一个布尔变量,该变量将检查清单中的权限,如下所示:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
SharedPreferences prefs = getActivity().getSharedPreferences("ShortCutPrefs", 0);
if (!prefs.getBoolean("isPermitted", false)) {
if (per == false) {
if ((getActivity().checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)) {
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
return;
}
}
} else {
if (per == false) {
if ((getActivity().checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)) {
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
return;
}
}
和权限检查部分:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SharedPreferences prefs = getActivity().getSharedPreferences("ShortCutPrefs", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("isPermitted", true);
editor.commit();
per = true;
setData();
} else {
Toast.makeText(getActivity(), "Permission denied to check you device details, grant this app permission", Toast.LENGTH_LONG).show();
}
return;
}
}
}
通过这种方式,您可以检查某人是否已从设置中删除了权限,并在应用开始时询问他们。
祝你好运!