我正在尝试检查我的应用是否有权访问存储空间:
if (ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getBaseContext(), "merry christmas", Toast.LENGTH_LONG).show();
}
这仅在清单中没有存储权限时才有效。当我添加权限时,它停止显示烤面包。似乎此代码检查清单是否具有权限,而不是是否授予了权限。我该如何解决??
答案 0 :(得分:1)
您必须使用以下代码手动请求用户允许权限。然后检查是否授予许可。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(permitted()){
// prompting permission dialog
// permform your task
}
}
@Override
public void onResume() {
super.onResume();
if (permitted(PERMISSIONS))
load();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (permitted(permissions)) {
load();
} else {
Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
}
}
}
public static final String[] PERMISSIONS = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
boolean permitted(String[] ss) {
for (String s : ss) {
if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
boolean permitted() {
for (String s : PERMISSIONS) {
if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, 1);
return false;
}
}
return true;
}
答案 1 :(得分:0)
您的代码仅在未授予许可的情况下有效。如果未运行,则在安装过程中或从以前的apk数据中授予该权限。只需进入设置,选择您的应用并撤消权限,或者选择清除数据选项,然后重试。
答案 2 :(得分:0)
这就是我的方法
if (!checkPermission()) { // this line checks permission everytime you access this activity
Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
requestPermission();
} else {
enableGPS();
}
public boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION);
return result == PackageManager.PERMISSION_GRANTED;
}
public void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
enableGPS();
} else {
Toast.makeText(getApplicationContext(), "Please allow access to continue!", Toast.LENGTH_SHORT).show();
}
}
}
答案 3 :(得分:0)
在应用程序级别gradle中添加以下依赖项,并同步您的项目。
implementaion 'com.nabinbhandari.android:permissions:3.8'
在此之后,在您的活动中编写代码以检查是否授予了权限。
public void requestCameraAndStorage() {
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA}; // dont forget to add these permissions in manifest file.
Permissions.check(this, permissions, null, null, new PermissionHandler() {
@Override
public void onGranted() {
// do whatever you want to do.
}
@Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
super.onDenied(context, deniedPermissions);
}
});
}
然后根据需要在onCreate
或任何onButtonClick
内调用此方法。此库非常有效地处理onDenied
。