我有一个对话框询问用户是否要允许或拒绝该应用访问手机文件系统的权限。拒绝后,没有任何事情发生,这是预期的功能。但是,在允许的情况下,似乎某些代码行没有被执行。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.i("DESIRED ACTION", String.valueOf(DESIRED_ACTION) + " " + String.valueOf(DESIRED_ACTION == ACTION_ADD_ENTRY));
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("Testing", String.valueOf(DESIRED_ACTION == ACTION_ADD_ENTRY));
if (DESIRED_ACTION == ACTION_ADD_ENTRY) {
Log.i("Testing", "Testing");
addEntry();
}
if (DESIRED_ACTION == ACTION_ADD_ENTRY) {
Log.i("Permissions", "Writing granted");
}
}
// else if not permission granted
break;
}
}
}
public void addEntry() {
Log.i("Invocation", "ADD ENTRY");
}
带有信息过滤器的日志的相关部分仅显示以下内容:
.../DESIRED ACTION: 1 true
.../Permissions: Writing granted
在日志中显示Permissions: Writing granted
,表示DESIRED_ACTION == ACTION_ADD_ENTRY
。但是,如果是这样的话,if语句中的代码行是否也应该运行,导致Testing: Testing
和Invocation: ADD ENTRY
也出现在日志中?
我不明白这里发生了什么。为什么有些代码被省略?