Permissions Not Persistent During Across App Startups
-
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 modelpublic 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 thismPermissionManagerInterface = new PermissionManagerInterface() { @Override public void onPermissionGranted(String mes
-
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 modelpublic 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 thismPermissionManagerInterface = new PermissionManagerInterface() { @Override public void onPermissionGranted(String mes
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 !~
-
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 !~
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?? -
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??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 !~
-
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 !~
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.
-
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.
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