我正在开发一个Android应用程序,并且要求权限我正在使用Easy Permissions library。一切正常,直到用户拒绝设置中的任何权限。
我正在使用带有Android Nougat(7.0)的三星Galaxy S6测试应用程序,我不知道这是否是系统问题,或者我在代码中做错了什么。
这是我的MainActivity类,其中实现了EasyPermission库:
public class MainActivity extends AppCompatActivity implements
EasyPermissions.RationaleCallbacks,
EasyPermissions.PermissionCallbacks{
private static String[] PERMS =
{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.GET_ACCOUNTS};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(EasyPermissions.hasPermissions(this, PERMS)){
create();
} else {
ActivityCompat.requestPermissions(this, PERMS, 0);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode,
permissions,
grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode,
permissions,
grantResults,
this);
}
@Override
public void onPermissionsGranted(int requestCode,
@NonNull List<String> perms) {
if(EasyPermissions.hasPermissions(getApplication(), PERMS)) {
create();
}
}
@Override
public void onPermissionsDenied(int requestCode,
@NonNull List<String> perms) {
finishAffinity();
}
@Override
public void onRationaleAccepted(int requestCode) {
}
@Override
public void onRationaleDenied(int requestCode) {
}
public void create() { ... }
}
答案 0 :(得分:1)
在onRequestPermissionsResult
中,您再次呼叫EasyPermissions,这会产生一个循环。将您的onRequestPermissionsResult
更改为此:
int permissionCheck = PackageManager.PERMISSION_GRANTED;
for (int permission : grantResults) {
permissionCheck = permissionCheck + permission;
}
if ((grantResults.length > 0) && permissionCheck == PackageManager.PERMISSION_GRANTED) {
create()
}