我有一个导航抽屉。在导航抽屉中,我正在为片段充气。在第一个片段中,我向用户请求运行时权限。但在第二个片段中,我想再次要求用户获得相同的权限。但在我的情况下,第一个片段请求许可。但在第二个片段中,它没有请求许可。所以谁能告诉我是什么问题?因为我在这两种情况下都使用相同的代码。
这是我的第一个片段代码:-
if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION, getActivity().getApplicationContext(), getActivity())) {
IsConsumed = 1;
fetchLocationData();
} else {
requestPermission(Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_REQUEST_CODE_LOCATION, getActivity().getApplicationContext(), getActivity());
}
public void requestPermission(String strPermission, int perCode, Context _c, Activity _a) {
if (ActivityCompat.shouldShowRequestPermissionRationale(_a, strPermission)) {
Toast.makeText(getActivity(), "GPS permission allows us to access location data. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(_a, new String[]{strPermission}, perCode);
}
}
public static boolean checkPermission(String strPermission, Context _c, Activity _a) {
int result = ContextCompat.checkSelfPermission(_c, strPermission);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
fetchLocationData();
} else {
Toast.makeText(getActivity(), "Permission Denied, You cannot access location data.", Toast.LENGTH_LONG).show();
}
break;
}
}
我在第二个片段中使用相同的代码。但它不要求许可。我试图打印它进入 if() 条件的日志。并打印日志。但它不要求许可。