如何检查片段的权限

我想检查一个片段内的权限。

我的代码:

// Here, thisActivity is the current activity if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION)) { // Show an explanation to the user *asynchronously* -- don't block // this thread waiting for the user's response! After the user // sees the explanation, try again to request the permission. } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. } } 

但在允许或拒绝之后不会调用onRequestPermissionsResult

 @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 1: { Log.e("test","0"); // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. //yes Log.e("test","1"); Intent intent = new Intent(getActivity(), MapsActivity.class); intent.putExtra("latitude", 35.694828); intent.putExtra("longitude", 51.378129); startActivity(intent); } else { utilityFunctions.showSweetAlertWarning(getActivity(),r.getString(R.string.str_warning_title_empty), r.getString(R.string.str_you_must_allow_this_permission_toast), r.getString(R.string.str_warning_btn_login)); Log.e("test","2"); } return; } // other 'case' lines to check for other // permissions this app might request } } 

Solutions Collecting From Web of "如何检查片段的权限"

我做了以下检查一个片段内的权限。

 if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION); } else { Log.e("DB", "PERMISSION GRANTED"); } 

我就是这样做的,这对我很有用。 谢谢!

对于活动:

 ActivityCompat.requestPermissions(this, permissionsList, REQUEST_CODE); 

对于片段:

 requestPermissions(permissionsList, REQUEST_CODE); 

片段有requestPermissions()onRequestPermissionsResult()方法,使用它。

但是checkSelfPermission()是来自ActivityCompat (不需要Activity ,只有Context )。

 if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { requestPermissions( //Method of Fragment String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSIONS_CODE_WRITE_STORAGE ); } else { downloadImage(); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQUEST_PERMISSIONS_CODE_WRITE_STORAGE) { if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResults[0] == PackageManager.PERMISSION_GRANTED) { proceedWithSdCard(); } } } 

onRequestPermissionsResult在活动而不是片段中被调用。 尝试在活动中覆盖onRequestPermissionsResult

如果您从设置中closures了应用程序的许可,则不能从代码或Android版本低于棉花糖打开您的许可。

你可以查看这个文档https://developer.android.com/training/permissions/requesting.html这是一个例子https://www.learn2crack.com/2015/10/android-marshmallow-permissions.html

请参阅此链接它为您提供了处理运行时权限的完整指南。 即使获得布尔值来检查是否允许权限或只是你想要的。