Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. Permissions Not Persistent During Across App Startups

Permissions Not Persistent During Across App Startups

Scheduled Pinned Locked Moved Android
help
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    Hello there. First of all, permission working fine when I use onRequestPermissionsResult() in MainActivity. Problem occurs when I try to seperate permission from MainActivity using the following model

    public interface RequestPermissionsResultInterface
    {
    void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults);
    }

    public interface PermissionManagerInterface
    {
    void onPermissionGranted(String message, int requestCode);
    void onPermissionDenied(String message, int requestCode);
    }

    public class PermissionManager implements RequestPermissionsResultInterface
    {
    private Activity mActivity;
    private static Context mContext;
    private static PermissionManager mPermissionManager;
    private static volatile PermissionManagerInterface mManagerInterface;

    public static PermissionManager getInstance(Context context)
    {
        if (mPermissionManager == null)
        {
            synchronized (PermissionManager.class)
            {
                if (mPermissionManager == null)
                {
                    mPermissionManager = new PermissionManager(context);
                }
            }
        }
        return mPermissionManager;
    }
    
    public PermissionManager(Context context) {
        mContext = context;
    }
    
    public RequestPermissionsResultInterface askPermission(
            Activity mActivity,
            String permissionName,
            final PermissionManagerInterface managerInterface,
            final int requestCode)
    {
       if (ContextCompat.checkSelfPermission(mActivity, permissionName) != PackageManager.PERMISSION\_GRANTED)
       {
          // show message box
       }
       else
          ActivityCompat.requestPermissions(
                      mActivity,
                      new String\[\]{permissionName},
                      requestCode);
    }
    

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
    {
    if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
    mManagerInterface.onPermissionGranted("Permission Granted", requestCode);
    else
    mManagerInterface.onPermissionDenied("Permission Denied", requestCode);
    }
    }

    Then I use PermissionManager in the MainActivity::OnCreate() like this

        mPermissionManagerInterface = new PermissionManagerInterface() {
            @Override
            public void onPermissionGranted(String mes
    
    A 1 Reply Last reply
    0
    • D Django_Untaken

      Hello there. First of all, permission working fine when I use onRequestPermissionsResult() in MainActivity. Problem occurs when I try to seperate permission from MainActivity using the following model

      public interface RequestPermissionsResultInterface
      {
      void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults);
      }

      public interface PermissionManagerInterface
      {
      void onPermissionGranted(String message, int requestCode);
      void onPermissionDenied(String message, int requestCode);
      }

      public class PermissionManager implements RequestPermissionsResultInterface
      {
      private Activity mActivity;
      private static Context mContext;
      private static PermissionManager mPermissionManager;
      private static volatile PermissionManagerInterface mManagerInterface;

      public static PermissionManager getInstance(Context context)
      {
          if (mPermissionManager == null)
          {
              synchronized (PermissionManager.class)
              {
                  if (mPermissionManager == null)
                  {
                      mPermissionManager = new PermissionManager(context);
                  }
              }
          }
          return mPermissionManager;
      }
      
      public PermissionManager(Context context) {
          mContext = context;
      }
      
      public RequestPermissionsResultInterface askPermission(
              Activity mActivity,
              String permissionName,
              final PermissionManagerInterface managerInterface,
              final int requestCode)
      {
         if (ContextCompat.checkSelfPermission(mActivity, permissionName) != PackageManager.PERMISSION\_GRANTED)
         {
            // show message box
         }
         else
            ActivityCompat.requestPermissions(
                        mActivity,
                        new String\[\]{permissionName},
                        requestCode);
      }
      

      @Override
      public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
      {
      if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
      mManagerInterface.onPermissionGranted("Permission Granted", requestCode);
      else
      mManagerInterface.onPermissionDenied("Permission Denied", requestCode);
      }
      }

      Then I use PermissionManager in the MainActivity::OnCreate() like this

          mPermissionManagerInterface = new PermissionManagerInterface() {
              @Override
              public void onPermissionGranted(String mes
      
      A Offline
      A Offline
      Afzaal Ahmad Zeeshan
      wrote on last edited by
      #2

      Did you ask for these permissions in the manifest file? Permissions granted there, are typically available for the device and you can "rely" on the fact that they are granted. Since you are requesting the permission on runtime, during the session it is likely that your application will require to request it again. Also, note that moving upwards from Android 6.0 users can accept or deny permissions granted during install time as well, so there is very less that you can do other than PermissionManager object to always check if you have permissions, otherwise it will always end up with a runtime error. [Control your app permissions on Android 6.0 and up - Google Play Help](https://support.google.com/googleplay/answer/6270602?hl=en) There are even facts more to the story, read, [Everything every Android Developer must know about new Android's Runtime Permission :: The Cheese Factory](https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en).

      Post above:

      In Android 6.0 Marshmallow, application will not be granted any permission at installation time. Instead, application has to ask user for a permission one-by-one at runtime.

      In other words, it is the only way to go as it guarantees that the code will execute in the case when you have the permission. Also, this allows users to enable permissions by default in the settings, so in case your users are loving your application and they want to grant it some permissions they can (through this permissions model) go to the settings and allow permissions for the features they want your application to always work; read the Google Play Help link I provided. Not recommended approach: You can create a singleton pattern in Android application and then request the permissions on the startup, then during the entire Android's awake period, it will have the permission but upon a restart it will request them again. But nonetheless, this is the least or not at all recommended approach from me.

      The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

      D 1 Reply Last reply
      0
      • A Afzaal Ahmad Zeeshan

        Did you ask for these permissions in the manifest file? Permissions granted there, are typically available for the device and you can "rely" on the fact that they are granted. Since you are requesting the permission on runtime, during the session it is likely that your application will require to request it again. Also, note that moving upwards from Android 6.0 users can accept or deny permissions granted during install time as well, so there is very less that you can do other than PermissionManager object to always check if you have permissions, otherwise it will always end up with a runtime error. [Control your app permissions on Android 6.0 and up - Google Play Help](https://support.google.com/googleplay/answer/6270602?hl=en) There are even facts more to the story, read, [Everything every Android Developer must know about new Android's Runtime Permission :: The Cheese Factory](https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en).

        Post above:

        In Android 6.0 Marshmallow, application will not be granted any permission at installation time. Instead, application has to ask user for a permission one-by-one at runtime.

        In other words, it is the only way to go as it guarantees that the code will execute in the case when you have the permission. Also, this allows users to enable permissions by default in the settings, so in case your users are loving your application and they want to grant it some permissions they can (through this permissions model) go to the settings and allow permissions for the features they want your application to always work; read the Google Play Help link I provided. Not recommended approach: You can create a singleton pattern in Android application and then request the permissions on the startup, then during the entire Android's awake period, it will have the permission but upon a restart it will request them again. But nonetheless, this is the least or not at all recommended approach from me.

        The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

        D Offline
        D Offline
        Django_Untaken
        wrote on last edited by
        #3

        Afzaal Ahmad Zeeshan wrote:

        Not recommended approach: You can create a singleton pattern in Android application and then ....

        You are absolutely right. I am following this pattern. I don't exactly know why but I am using it (See PermissionManager). I guess I wanted to seperate this part from MainActivity. Question is: WHY IS IT DOING IT? WHY IT IS ASKING FOR PERMISSIONS AGAIN???? WHAT IS THE SOLUTION??

        A 1 Reply Last reply
        0
        • D Django_Untaken

          Afzaal Ahmad Zeeshan wrote:

          Not recommended approach: You can create a singleton pattern in Android application and then ....

          You are absolutely right. I am following this pattern. I don't exactly know why but I am using it (See PermissionManager). I guess I wanted to seperate this part from MainActivity. Question is: WHY IS IT DOING IT? WHY IT IS ASKING FOR PERMISSIONS AGAIN???? WHAT IS THE SOLUTION??

          A Offline
          A Offline
          Afzaal Ahmad Zeeshan
          wrote on last edited by
          #4

          Django_Untaken wrote:

          Question is: WHY IS IT DOING IT? WHY IT IS ASKING FOR PERMISSIONS AGAIN???? WHAT IS THE SOLUTION??

          Instead of shouting, (which is never going to work here as we are not bound to do anything), just re-read the reply.

          The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

          D 1 Reply Last reply
          0
          • A Afzaal Ahmad Zeeshan

            Django_Untaken wrote:

            Question is: WHY IS IT DOING IT? WHY IT IS ASKING FOR PERMISSIONS AGAIN???? WHAT IS THE SOLUTION??

            Instead of shouting, (which is never going to work here as we are not bound to do anything), just re-read the reply.

            The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

            D Offline
            D Offline
            Django_Untaken
            wrote on last edited by
            #5

            Afzaal Ahmad Zeeshan wrote:

            Instead of shouting,...

            I did not shout. I would never. I just capitalized my questions. In the last 10 years as codeproject user, I never saw "A" telling "B" not to shout just because "B" wrote questions in capitals. However things can be misinterpreted when in written form.

            Richard DeemingR 1 Reply Last reply
            0
            • D Django_Untaken

              Afzaal Ahmad Zeeshan wrote:

              Instead of shouting,...

              I did not shout. I would never. I just capitalized my questions. In the last 10 years as codeproject user, I never saw "A" telling "B" not to shout just because "B" wrote questions in capitals. However things can be misinterpreted when in written form.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Django_Untaken wrote:

              I did not shout. ... I just capitalized my questions.

              Typing in ALL CAPS is the internet equivalent of shouting. This is common knowledge, and applies to the entire internet, not just CodeProject.

              Django_Untaken wrote:

              In the last 10 years as codeproject user, I never saw "A" telling "B" not to shout just because "B" wrote questions in capitals.

              Then you haven't been looking very hard. Every time someone posts a question in ALL CAPS, they have quickly been rebuked for shouting and, where possible, their message has been edited to the correct case.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups