0

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();
        }
    }
}

2 Answers2

2

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.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • It seems there is something fundamentally wrong with checking for multiple permissions at once. I implemented the code in your link, and now it works like a charm, thanks :) – Richard Slettevoll Jun 20 '18 at 09:50
2

AS per my above comment

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

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • you post same answer @Sagar just add some posts why? – Adil Jun 20 '18 at 08:41
  • @AD10 have u check the code just check again my friend you will find the difference – AskNilesh Jun 20 '18 at 08:42
  • 1
    @AD10 we both have given ans at the same time just one min difference so you can not say that same answer – AskNilesh Jun 20 '18 at 08:46
  • @AD10 you can also check edit history for both answer – AskNilesh Jun 20 '18 at 08:55
  • 1
    @AD10 the time difference between these two answers is so minimal, so I don't think foul play can be assumed, going off the edit history, the code you both posted in each of the first revisions seems identical, I think its just a coincidence that you both posted the same code – WhatsThePoint Jun 20 '18 at 09:12
  • This question/answers are [discussed on meta](https://meta.stackoverflow.com/questions/369821/is-it-wrong-to-add-the-same-answer-that-another-user-posts-at-the-same-time). – BDL Jun 20 '18 at 09:28