[英]How to check if app has a specific permission?
I am trying to check if my app has access to storage: 我正在尝试检查我的应用是否有权访问存储:
if (ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getBaseContext(), "merry christmas", Toast.LENGTH_LONG).show();
}
this only works when the storage permission is not in the manifest. 仅当清单中没有存储权限时,此方法才有效。 It stops showing toast when i add the permission. 当我添加权限时,它停止显示烤面包。 Seems like this code checks if the manifest has the permission instead of if the permission is granted. 似乎此代码检查清单是否具有权限,而不是是否授予了权限。 How do I fix this??? 我该如何解决???
You have to manually ask the user for allowing permission with following code. 您必须使用以下代码手动请求用户允许权限。 And then check that if permission is granted or not. 然后检查是否授予许可。
@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;
}
Your code only works if the permission is not granted. 您的代码仅在未授予许可的情况下有效。 If it didn't run then that permission was granted either during installation or from the data of the previous apk. 如果未运行,则在安装过程中或从以前的apk数据中授予该权限。 Just go to settings, choose your app and revoke the permission or select the clear data option and try again. 只需转到设置,选择您的应用并撤消权限,或选择清除数据选项,然后重试。
This is how I do it 这就是我的方法
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();
}
}
}
Add the following dependency in app level gradle and sync your project. 在应用程序级别gradle中添加以下依赖项,并同步您的项目。
implementaion 'com.nabinbhandari.android:permissions:3.8'
After that inside your activity write the code to check if permission is granted. 之后,在您的活动中编写代码以检查是否授予了权限。
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);
}
});
}
And call this method inside onCreate
or at any onButtonClick
as want.This library handles onDenied
very efficiently. 并根据需要在onCreate
或任何onButtonClick
调用此方法。此库非常有效地处理onDenied
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.