How to check if app has a specific permission?












0















I am trying to check if my app has access to storage:



 if (ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getBaseContext(), "merry christmas", Toast.LENGTH_LONG).show();
}


this only works when the storage permission is not in the manifest. It stops showing toast when i add the permission. Seems like this code checks if the manifest has the permission instead of if the permission is granted. How do I fix this???










share|improve this question























  • testing on android 9.0

    – user9555243
    Nov 24 '18 at 6:30











  • you should have READ_EXTERNAL_STORAGE pwemission in Manifest.xml

    – Ali Ahmed
    Nov 24 '18 at 6:35


















0















I am trying to check if my app has access to storage:



 if (ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getBaseContext(), "merry christmas", Toast.LENGTH_LONG).show();
}


this only works when the storage permission is not in the manifest. It stops showing toast when i add the permission. Seems like this code checks if the manifest has the permission instead of if the permission is granted. How do I fix this???










share|improve this question























  • testing on android 9.0

    – user9555243
    Nov 24 '18 at 6:30











  • you should have READ_EXTERNAL_STORAGE pwemission in Manifest.xml

    – Ali Ahmed
    Nov 24 '18 at 6:35
















0












0








0








I am trying to check if my app has access to storage:



 if (ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getBaseContext(), "merry christmas", Toast.LENGTH_LONG).show();
}


this only works when the storage permission is not in the manifest. It stops showing toast when i add the permission. Seems like this code checks if the manifest has the permission instead of if the permission is granted. How do I fix this???










share|improve this question














I am trying to check if my app has access to storage:



 if (ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getBaseContext(), "merry christmas", Toast.LENGTH_LONG).show();
}


this only works when the storage permission is not in the manifest. It stops showing toast when i add the permission. Seems like this code checks if the manifest has the permission instead of if the permission is granted. How do I fix this???







android permissions






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 6:27









user9555243user9555243

408




408













  • testing on android 9.0

    – user9555243
    Nov 24 '18 at 6:30











  • you should have READ_EXTERNAL_STORAGE pwemission in Manifest.xml

    – Ali Ahmed
    Nov 24 '18 at 6:35





















  • testing on android 9.0

    – user9555243
    Nov 24 '18 at 6:30











  • you should have READ_EXTERNAL_STORAGE pwemission in Manifest.xml

    – Ali Ahmed
    Nov 24 '18 at 6:35



















testing on android 9.0

– user9555243
Nov 24 '18 at 6:30





testing on android 9.0

– user9555243
Nov 24 '18 at 6:30













you should have READ_EXTERNAL_STORAGE pwemission in Manifest.xml

– Ali Ahmed
Nov 24 '18 at 6:35







you should have READ_EXTERNAL_STORAGE pwemission in Manifest.xml

– Ali Ahmed
Nov 24 '18 at 6:35














3 Answers
3






active

oldest

votes


















1














You have to manually ask the user for allowing permission with following code. And then check that if permission is granted or not.



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(permitted()){
// prompting permission dialog
// permform your task
}

}


@Override
public void onResume() {
super.onResume();
if (permitted(PERMISSIONS))
load();
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (permitted(permissions)) {
load();
} else {
Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
}
}
}
public static final String PERMISSIONS = new String{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

boolean permitted(String ss) {
for (String s : ss) {
if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}

boolean permitted() {
for (String s : PERMISSIONS) {
if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, 1);
return false;
}
}
return true;
}





share|improve this answer





















  • 1





    This is a good way but it only checks if the user clicked on allow or deny button , otherwise it is not called

    – Kevin Kurien
    Nov 24 '18 at 7:07






  • 1





    @KevinKurien you have to call the permitted() method inside oncreate method like if(permitted){}

    – Raza
    Nov 24 '18 at 7:12











  • Sorry I didn't see that :)

    – Kevin Kurien
    Nov 24 '18 at 7:14











  • just updated my answer

    – Raza
    Nov 24 '18 at 7:16











  • yeah I see it now , I upvoted! :)

    – Kevin Kurien
    Nov 24 '18 at 7:17



















0














Your code only works if the permission is not granted. If it didn't run then that permission was granted either during installation or from the data of the previous apk. Just go to settings, choose your app and revoke the permission or select the clear data option and try again.






share|improve this answer
























  • I never granted the permission, nor have the mechanism/code to request permissions. also double checked in settings, storage permission has not been granted.

    – user9555243
    Nov 24 '18 at 6:34











  • @user9555243 I think only read permission doesn't show up in the settings, add the write external storage permission in the manifest and add that in the code as well and see what happens.

    – RoyalGriffin
    Nov 24 '18 at 6:43



















0














This is how I do it



   if (!checkPermission()) {  // this line checks permission everytime you access this activity
Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
requestPermission();
} else {
enableGPS();
}




public boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION);
return result == PackageManager.PERMISSION_GRANTED;
}

public void requestPermission() {
ActivityCompat.requestPermissions(this, new String{ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {
if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
enableGPS();
} else {
Toast.makeText(getApplicationContext(), "Please allow access to continue!", Toast.LENGTH_SHORT).show();
}
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455749%2fhow-to-check-if-app-has-a-specific-permission%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You have to manually ask the user for allowing permission with following code. And then check that if permission is granted or not.



    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(permitted()){
    // prompting permission dialog
    // permform your task
    }

    }


    @Override
    public void onResume() {
    super.onResume();
    if (permitted(PERMISSIONS))
    load();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
    case 1:
    if (permitted(permissions)) {
    load();
    } else {
    Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
    }
    }
    }
    public static final String PERMISSIONS = new String{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    boolean permitted(String ss) {
    for (String s : ss) {
    if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
    return false;
    }
    }
    return true;
    }

    boolean permitted() {
    for (String s : PERMISSIONS) {
    if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, 1);
    return false;
    }
    }
    return true;
    }





    share|improve this answer





















    • 1





      This is a good way but it only checks if the user clicked on allow or deny button , otherwise it is not called

      – Kevin Kurien
      Nov 24 '18 at 7:07






    • 1





      @KevinKurien you have to call the permitted() method inside oncreate method like if(permitted){}

      – Raza
      Nov 24 '18 at 7:12











    • Sorry I didn't see that :)

      – Kevin Kurien
      Nov 24 '18 at 7:14











    • just updated my answer

      – Raza
      Nov 24 '18 at 7:16











    • yeah I see it now , I upvoted! :)

      – Kevin Kurien
      Nov 24 '18 at 7:17
















    1














    You have to manually ask the user for allowing permission with following code. And then check that if permission is granted or not.



    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(permitted()){
    // prompting permission dialog
    // permform your task
    }

    }


    @Override
    public void onResume() {
    super.onResume();
    if (permitted(PERMISSIONS))
    load();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
    case 1:
    if (permitted(permissions)) {
    load();
    } else {
    Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
    }
    }
    }
    public static final String PERMISSIONS = new String{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    boolean permitted(String ss) {
    for (String s : ss) {
    if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
    return false;
    }
    }
    return true;
    }

    boolean permitted() {
    for (String s : PERMISSIONS) {
    if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, 1);
    return false;
    }
    }
    return true;
    }





    share|improve this answer





















    • 1





      This is a good way but it only checks if the user clicked on allow or deny button , otherwise it is not called

      – Kevin Kurien
      Nov 24 '18 at 7:07






    • 1





      @KevinKurien you have to call the permitted() method inside oncreate method like if(permitted){}

      – Raza
      Nov 24 '18 at 7:12











    • Sorry I didn't see that :)

      – Kevin Kurien
      Nov 24 '18 at 7:14











    • just updated my answer

      – Raza
      Nov 24 '18 at 7:16











    • yeah I see it now , I upvoted! :)

      – Kevin Kurien
      Nov 24 '18 at 7:17














    1












    1








    1







    You have to manually ask the user for allowing permission with following code. And then check that if permission is granted or not.



    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(permitted()){
    // prompting permission dialog
    // permform your task
    }

    }


    @Override
    public void onResume() {
    super.onResume();
    if (permitted(PERMISSIONS))
    load();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
    case 1:
    if (permitted(permissions)) {
    load();
    } else {
    Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
    }
    }
    }
    public static final String PERMISSIONS = new String{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    boolean permitted(String ss) {
    for (String s : ss) {
    if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
    return false;
    }
    }
    return true;
    }

    boolean permitted() {
    for (String s : PERMISSIONS) {
    if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, 1);
    return false;
    }
    }
    return true;
    }





    share|improve this answer















    You have to manually ask the user for allowing permission with following code. And then check that if permission is granted or not.



    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(permitted()){
    // prompting permission dialog
    // permform your task
    }

    }


    @Override
    public void onResume() {
    super.onResume();
    if (permitted(PERMISSIONS))
    load();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
    case 1:
    if (permitted(permissions)) {
    load();
    } else {
    Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
    }
    }
    }
    public static final String PERMISSIONS = new String{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    boolean permitted(String ss) {
    for (String s : ss) {
    if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
    return false;
    }
    }
    return true;
    }

    boolean permitted() {
    for (String s : PERMISSIONS) {
    if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, 1);
    return false;
    }
    }
    return true;
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 24 '18 at 20:04









    Genetic Forest

    1,86331425




    1,86331425










    answered Nov 24 '18 at 7:03









    RazaRaza

    25912




    25912








    • 1





      This is a good way but it only checks if the user clicked on allow or deny button , otherwise it is not called

      – Kevin Kurien
      Nov 24 '18 at 7:07






    • 1





      @KevinKurien you have to call the permitted() method inside oncreate method like if(permitted){}

      – Raza
      Nov 24 '18 at 7:12











    • Sorry I didn't see that :)

      – Kevin Kurien
      Nov 24 '18 at 7:14











    • just updated my answer

      – Raza
      Nov 24 '18 at 7:16











    • yeah I see it now , I upvoted! :)

      – Kevin Kurien
      Nov 24 '18 at 7:17














    • 1





      This is a good way but it only checks if the user clicked on allow or deny button , otherwise it is not called

      – Kevin Kurien
      Nov 24 '18 at 7:07






    • 1





      @KevinKurien you have to call the permitted() method inside oncreate method like if(permitted){}

      – Raza
      Nov 24 '18 at 7:12











    • Sorry I didn't see that :)

      – Kevin Kurien
      Nov 24 '18 at 7:14











    • just updated my answer

      – Raza
      Nov 24 '18 at 7:16











    • yeah I see it now , I upvoted! :)

      – Kevin Kurien
      Nov 24 '18 at 7:17








    1




    1





    This is a good way but it only checks if the user clicked on allow or deny button , otherwise it is not called

    – Kevin Kurien
    Nov 24 '18 at 7:07





    This is a good way but it only checks if the user clicked on allow or deny button , otherwise it is not called

    – Kevin Kurien
    Nov 24 '18 at 7:07




    1




    1





    @KevinKurien you have to call the permitted() method inside oncreate method like if(permitted){}

    – Raza
    Nov 24 '18 at 7:12





    @KevinKurien you have to call the permitted() method inside oncreate method like if(permitted){}

    – Raza
    Nov 24 '18 at 7:12













    Sorry I didn't see that :)

    – Kevin Kurien
    Nov 24 '18 at 7:14





    Sorry I didn't see that :)

    – Kevin Kurien
    Nov 24 '18 at 7:14













    just updated my answer

    – Raza
    Nov 24 '18 at 7:16





    just updated my answer

    – Raza
    Nov 24 '18 at 7:16













    yeah I see it now , I upvoted! :)

    – Kevin Kurien
    Nov 24 '18 at 7:17





    yeah I see it now , I upvoted! :)

    – Kevin Kurien
    Nov 24 '18 at 7:17













    0














    Your code only works if the permission is not granted. If it didn't run then that permission was granted either during installation or from the data of the previous apk. Just go to settings, choose your app and revoke the permission or select the clear data option and try again.






    share|improve this answer
























    • I never granted the permission, nor have the mechanism/code to request permissions. also double checked in settings, storage permission has not been granted.

      – user9555243
      Nov 24 '18 at 6:34











    • @user9555243 I think only read permission doesn't show up in the settings, add the write external storage permission in the manifest and add that in the code as well and see what happens.

      – RoyalGriffin
      Nov 24 '18 at 6:43
















    0














    Your code only works if the permission is not granted. If it didn't run then that permission was granted either during installation or from the data of the previous apk. Just go to settings, choose your app and revoke the permission or select the clear data option and try again.






    share|improve this answer
























    • I never granted the permission, nor have the mechanism/code to request permissions. also double checked in settings, storage permission has not been granted.

      – user9555243
      Nov 24 '18 at 6:34











    • @user9555243 I think only read permission doesn't show up in the settings, add the write external storage permission in the manifest and add that in the code as well and see what happens.

      – RoyalGriffin
      Nov 24 '18 at 6:43














    0












    0








    0







    Your code only works if the permission is not granted. If it didn't run then that permission was granted either during installation or from the data of the previous apk. Just go to settings, choose your app and revoke the permission or select the clear data option and try again.






    share|improve this answer













    Your code only works if the permission is not granted. If it didn't run then that permission was granted either during installation or from the data of the previous apk. Just go to settings, choose your app and revoke the permission or select the clear data option and try again.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '18 at 6:33









    RoyalGriffinRoyalGriffin

    8071414




    8071414













    • I never granted the permission, nor have the mechanism/code to request permissions. also double checked in settings, storage permission has not been granted.

      – user9555243
      Nov 24 '18 at 6:34











    • @user9555243 I think only read permission doesn't show up in the settings, add the write external storage permission in the manifest and add that in the code as well and see what happens.

      – RoyalGriffin
      Nov 24 '18 at 6:43



















    • I never granted the permission, nor have the mechanism/code to request permissions. also double checked in settings, storage permission has not been granted.

      – user9555243
      Nov 24 '18 at 6:34











    • @user9555243 I think only read permission doesn't show up in the settings, add the write external storage permission in the manifest and add that in the code as well and see what happens.

      – RoyalGriffin
      Nov 24 '18 at 6:43

















    I never granted the permission, nor have the mechanism/code to request permissions. also double checked in settings, storage permission has not been granted.

    – user9555243
    Nov 24 '18 at 6:34





    I never granted the permission, nor have the mechanism/code to request permissions. also double checked in settings, storage permission has not been granted.

    – user9555243
    Nov 24 '18 at 6:34













    @user9555243 I think only read permission doesn't show up in the settings, add the write external storage permission in the manifest and add that in the code as well and see what happens.

    – RoyalGriffin
    Nov 24 '18 at 6:43





    @user9555243 I think only read permission doesn't show up in the settings, add the write external storage permission in the manifest and add that in the code as well and see what happens.

    – RoyalGriffin
    Nov 24 '18 at 6:43











    0














    This is how I do it



       if (!checkPermission()) {  // this line checks permission everytime you access this activity
    Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
    requestPermission();
    } else {
    enableGPS();
    }




    public boolean checkPermission() {
    int result = ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION);
    return result == PackageManager.PERMISSION_GRANTED;
    }

    public void requestPermission() {
    ActivityCompat.requestPermissions(this, new String{ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {
    if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    enableGPS();
    } else {
    Toast.makeText(getApplicationContext(), "Please allow access to continue!", Toast.LENGTH_SHORT).show();
    }
    }
    }





    share|improve this answer




























      0














      This is how I do it



         if (!checkPermission()) {  // this line checks permission everytime you access this activity
      Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
      requestPermission();
      } else {
      enableGPS();
      }




      public boolean checkPermission() {
      int result = ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION);
      return result == PackageManager.PERMISSION_GRANTED;
      }

      public void requestPermission() {
      ActivityCompat.requestPermissions(this, new String{ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
      }

      @Override
      public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {
      if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
      enableGPS();
      } else {
      Toast.makeText(getApplicationContext(), "Please allow access to continue!", Toast.LENGTH_SHORT).show();
      }
      }
      }





      share|improve this answer


























        0












        0








        0







        This is how I do it



           if (!checkPermission()) {  // this line checks permission everytime you access this activity
        Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
        requestPermission();
        } else {
        enableGPS();
        }




        public boolean checkPermission() {
        int result = ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION);
        return result == PackageManager.PERMISSION_GRANTED;
        }

        public void requestPermission() {
        ActivityCompat.requestPermissions(this, new String{ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {
        if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        enableGPS();
        } else {
        Toast.makeText(getApplicationContext(), "Please allow access to continue!", Toast.LENGTH_SHORT).show();
        }
        }
        }





        share|improve this answer













        This is how I do it



           if (!checkPermission()) {  // this line checks permission everytime you access this activity
        Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
        requestPermission();
        } else {
        enableGPS();
        }




        public boolean checkPermission() {
        int result = ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION);
        return result == PackageManager.PERMISSION_GRANTED;
        }

        public void requestPermission() {
        ActivityCompat.requestPermissions(this, new String{ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {
        if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        enableGPS();
        } else {
        Toast.makeText(getApplicationContext(), "Please allow access to continue!", Toast.LENGTH_SHORT).show();
        }
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 7:11









        Kevin KurienKevin Kurien

        558212




        558212






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455749%2fhow-to-check-if-app-has-a-specific-permission%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python