Reputation: 49
I'm developing an app on two different computers, and using github to sync the project using Android Studios git tools. I can't get the permissions right after I ran my app from my secondary workstation.
It seems ActivityCompat.checkSelfPermission() returns true even if the app doesn't have the required permissions, which leads the app to skip the ActivityCompat.requestPermissions() method.
Android Studio asks me to reinstall the app every time I switch workstations, as the signatures doesn't match or something. I assume the app might remember the old permissions even if it haven't gotten new ones?
compileSdkVersion 27
minSdkVersion 19
targetSdkVersion 27
Why the hell is ActivityCompat.checkSelfPermission() returning true when I have no permissions?
I ask for all the permissions in my MainActivity in the oncreate() method:
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();
}
}
I then handle the response:
@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();
}
}
}
Upvotes: 0
Views: 515
Reputation: 69671
No need to ask runtime permission for INTERNET
because it is granted at the time of app installation
FYI
Dangerous permission are need to ask runtime from os marshmallow and above
The INTERNET
is normal
For more information please check this post Normal permissions and dangerous permissions
Try this
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();
}
}
to ask multiple runtime permission please check below post
Upvotes: 2
Reputation: 24907
Remove following check from your if condition:
ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET)!= PackageManager.PERMISSION_GRANTED
The above condition will be always evaluate to false
since Internet permission is classified as normal permission and is granted at installation time.
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();
}
}
One more thing, you should segregate the checking for CAMERA
& WRITE_EXTERNAL_STORAGE
permission. This is because, there could be case where CAMERA
permission is granted but WRITE_EXTERNAL_STORAGE
permission is not granted.
With your checking the condition will not satisfy hence WRITE_EXTERNAL_STORAGE
wont be asked.
Check out this SO for implementation details for better approach.
Upvotes: 2