-1
我见过SO问题Can you request permissions synchronously in Android Marshmallow (API 23)'s runtime permissions model?。答案是不。等待,直到在Android中收到请求结果
因此,我添加了一个代码如下(简体版):
public class MyActivity ... {
private boolean hasGotPermissionRequestResult = false;
@Override
public void onCreate(...) {
if (ContextCompat.checkSelfPermission(...) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermission(...);
while (!hasGotPermissionRequestResult) {}
}
}
@Override
public void onRequestPermissionResult(...) {
// whether granted or not
hasGotPermissionRequestResult = true;
}
}
但是,我不知道我的方法是否是好的,安全,高效。
'onCreate'中的while(!hasGotPermissionRequestResult){}'? ...基本上它不好,既不安全也不高效...并且prolly不起作用 – Selvin
经验法则:从不使用繁忙的等待循环。尤其不在UI线程上 –