我正在两台不同的计算机上开发一个应用程序,并使用github使用Android Studios git工具同步该项目。 从辅助工作站运行应用程序后,我无法立即获得许可。
即使应用程序没有必需的权限,似乎 ActivityCompat.checkSelfPermission()也会返回 true ,这会导致应用程序跳过 ActivityCompat。 requestPermissions()方法。
由于签名不匹配或其他原因,每次切换工作站时,Android Studio都会要求我重新安装该应用程序。我认为应用程序即使没有获得新的权限,也可能会记住旧的权限?
compileSdkVersion 27
minSdkVersion 19
targetSdkVersion 27
为什么我没有权限时 ActivityCompat.checkSelfPermission()返回true?
我通过oncreate()方法请求MainActivity中的所有权限:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.INTERNET}, 123);
Toast.makeText(this, "This app needs camera and storage permissions", Toast.LENGTH_LONG).show();
}else{
onCreateAfterPermission();
}
}
然后我处理响应:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 123) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
onCreateAfterPermission();
}else{
// close the app
Toast.makeText(this, "Sorry!!!, you can't use this app without granting permissions", Toast.LENGTH_LONG).show();
this.finish();
}
}
}
答案 0 :(得分:2)
从您的if条件中删除以下检查内容:
ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET)!= PackageManager.PERMISSION_GRANTED
由于Internet权限被归类为普通权限并在安装时被授予,因此上述条件将始终评估为false
。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
Toast.makeText(this, "This app needs camera and storage permissions", Toast.LENGTH_LONG).show();
}else{
onCreateAfterPermission();
}
}
还有一件事情,您应该将对CAMERA
和WRITE_EXTERNAL_STORAGE
权限的检查分开。这是因为,可能会授予CAMERA
权限而没有授予WRITE_EXTERNAL_STORAGE
权限。
通过您的检查,条件将不满足,因此不会询问WRITE_EXTERNAL_STORAGE
。
请查看此SO以获得实施细节,以获得更好的方法。
答案 1 :(得分:2)
无需向INTERNET
授予运行时权限,因为它是在安装应用程序时授予的
仅供参考
需要危险的权限才能从os棉花糖及更高版本中请求运行时
INTERNET
是正常的
有关更多信息,请查看此帖子 Normal permissions and dangerous permissions
尝试一下
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
Toast.makeText(this, "This app needs camera and storage permissions", Toast.LENGTH_LONG).show();
}else{
onCreateAfterPermission();
}
}
要获得多个运行时权限,请检查以下帖子