Simplifying check permissions in android
compile 'com.huanjulu.android:permissionhandler:1.0.2'
- With this library, you can easily check for a series of related permissions on a project.
- Using this library, you can easily apply the permissions you need, such as Camera permission, Storage Permisson... And so on.
- You can easily check the user for the "Never ask Again" option. And you can customize your future operations.
- 利用这个library , 你可以很方便的在项目中检查是否拥有一系列的相关权限
- 利用这个library , 你可以很方便的去申请你所需要的权限,比如Camera permission, Storage Permisson ...等等.
- 你可以很方便的检查用户是够勾选了 “Never ask Again”选项。并且你可以自定义你以后的操作.
- Import a HTML file and watch it magically convert to Markdown
You nees decelar the permission what you need in your "AndroidManifest.xml" file. like
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
//ask camera permission
PermissionHandler.requestPermission(this, AppPermission.CAMERA.INSTANCE);
//ask storage permission
PermissionHandler.requestPermission(this, AppPermission.READ_EXTERNAL_STORAGE.INSTANCE);
if you do this, the first thing what you need to do is that overrive the onRequestPermissionsResult funtion ,like this
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
PermissionHandler.onRequestPermissionsResultReceived(requestCode, permissions, grantResults, appPermissionGranted -> {
Toast.makeText(this, " Permission Granted", Toast.LENGTH_SHORT).show();
// you select granted
return null;
}, appPermissionDenied -> {
Toast.makeText(this, " Permission Defined", Toast.LENGTH_SHORT).show();
// you select defined
return null;
});
}
here is some code what check camera permsiion, storage permission..so on
private void checkCameraPermission() {
PermissionHandler.checkPermission(this, AppPermission.CAMERA.INSTANCE,
//this permission was granted already
granted -> {
Toast.makeText(this, "Camera Permission Granted", Toast.LENGTH_SHORT).show();
return null;
},
//this permission was denied already
denied -> {
Toast.makeText(this, "Camera Permission Denied", Toast.LENGTH_SHORT).show();
return null;
},
//user select dot not ask again already
rationale -> {
Toast.makeText(this, "Camera Permission Not Ask Again", Toast.LENGTH_SHORT).show();
return null;
});
}
private void checkStoragePermission() {
PermissionHandler.checkPermission(this, AppPermission.READ_EXTERNAL_STORAGE.INSTANCE,
//this permission was granted already
granted -> {
Toast.makeText(this, "Storage Permission Granted", Toast.LENGTH_SHORT).show();
return null;
},
//this permission was denied already
denied -> {
Toast.makeText(this, "Storage PermissionDenied", Toast.LENGTH_SHORT).show();
return null;
},
//user select dot not ask again already
rationale -> {
Toast.makeText(this, "Storage Permission Not Ask Again", Toast.LENGTH_SHORT).show();
return null;
});
}
Copyright 2015 Anthony Restaino
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.