1

i am adding certain permissions to my app to allow access to camera for example and everything is working fine. but when i minimize the app and disable the permission then open the app, the app crashes without asking me to re-enable the permission(until i close the app and then open it). how can i fix this error so that the app dont crash and ask again for permission or safe restart to ask for permissions.

here is my code

in the main activity:

onCreate:

 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA},
                    GlobalVariables.MY_PERMISSIONS_REQUEST_CAMERA);
        }


@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode)
        {

            case GlobalVariables.MY_PERMISSIONS_REQUEST_CAMERA:
            {
                if (grantResults.length <= 0
                        || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    globalVariables.ShowOKAlert("Error","Please Accept All Requested Permissions or the app wont function properly",this,false);
                }
                return;
            }
        }
    }

the activity implements ActivityCompat.OnRequestPermissionsResultCallback

Rashad.Z
  • 2,494
  • 2
  • 27
  • 58

3 Answers3

1

step 1:

 String[] permissions = new String[]{ 
    Manifest.permission.INTERNET,
    Manifest.permission.READ_PHONE_STATE,
    Manifest.permission.READ_EXTERNAL_STORAGE,
    Manifest.permission.WRITE_EXTERNAL_STORAGE,
 };

Step 2:-

private boolean checkPermissions() {
    int result;
    List<String> listPermissionsNeeded = new ArrayList<>();
    for (String p : permissions) {
        result = ContextCompat.checkSelfPermission(this, p);
        if (result != PackageManager.PERMISSION_GRANTED) {
             listPermissionsNeeded.add(p);
        }
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), 100);
        return false;
    }
    return true;
}

Step 3:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    if (requestCode == 100) {
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // do something
        }
        return;
    }
}
0

You are checking that permission in onCreate. When you minimize and maximize the app again that method is not called.

Maybe you should check that permission in the extact point where you need it, or at least in onResume which surely is called again when you re-open your app.

Calling it in onResume ensure the app continues to work also if the user disables the permission in another window with the screen splitted (Android N feature).

Massimo
  • 3,436
  • 4
  • 40
  • 68
  • ok thanks will try that. but the error is appearing as soon as i maximize the app...i.e. i didnt actualy launch the camera yet and was still in the main activity – Rashad.Z Apr 18 '16 at 12:17
  • may you post the stack trace? – Massimo Apr 18 '16 at 12:18
  • in the stack trace, the error i am getting when disabling any permission i have (camera, location, storage) is java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.database.sqlite.SQLiteDatabase.isOpen()' on a null object reference which doesnt make sense – Rashad.Z Apr 18 '16 at 12:21
  • Ok, it seems unrelated to the permissions' management. Does it is thrown only if you disable the permission? Or also if you just minimize and maximize the app? – Massimo Apr 18 '16 at 12:24
  • you should post something more... at least the code with the exact row where the app crashes – Massimo Apr 18 '16 at 12:26
  • ok thanks man i will try and post something more useful once i find more info. – Rashad.Z Apr 18 '16 at 12:27
0

The easy way to achieve this to use direct static method of ActivityCompat

public static void requestPermissions(final @NonNull Activity activity,
            final @NonNull String[] permissions, final int requestCode)

Here you can see what parameters you have required for this

-- Activity Instance

-- String array of permissions

-- a user defined request code

Usage

Define global variables

//Run-time permissions for MarshMallow +
    public final String[] PERMISSION_ALL = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    //validateRequestPermissionsRequestCode requires requestCode to be of 8 bits, i.e. range: 0-255.
    public final int PERMISSION_REQUEST_CODE = 100;

Finally in your activity oncreate() method you can call static function mentioned above

public class MainActivity extends AppCompatActivity
{
    //your code here

     @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        //do other code work

        super.onCreate(savedInstanceState);

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.M)
        {
            ActivityCompat.requestPermissions(this, PERMISSION_ALL, PERMISSION_REQUEST_CODE);
        }

        //do other code work
    }

    //your code here
}

And you are done....

For more Info Click here about Android Permissions At Run Time

Zubair Ahmed
  • 2,857
  • 2
  • 27
  • 47