Allow Multiple Run Time Permission
Asked Answered
A

2

6

I am writing a code to ask for multiple run time permission on Android 6.0. I have followed some good example codes, but ActivityCompat.shouldShowRequestPermissionRationale (context, READ_PHONE_STATE) is causing an error that the first argument context is wrong.

How can I resolve this problem?

Thanks in advance.

Code is :

 if (ContextCompat
                    .checkSelfPermission(SpalshActivity.this,
                            READ_PHONE_STATE)+ContextCompat.checkSelfPermission(context,
                    WRITE_EXTERNAL_STORAGE) +ContextCompat.checkSelfPermission(context,
                    CAMERA) + ContextCompat
                    .checkSelfPermission(context,
                           READ_CONTACTS)+ContextCompat
                    .checkSelfPermission(context,
                            CALL_PHONE)+ContextCompat
                    .checkSelfPermission(context,
                            ACCESS_FINE_LOCATION)+ContextCompat
                    .checkSelfPermission(context,
                            READ_SMS)== PackageManager.PERMISSION_GRANTED) {
                myMethod();

            }
               else {
               if (ActivityCompat.shouldShowRequestPermissionRationale
                        (context, READ_PHONE_STATE) ||ActivityCompat.shouldShowRequestPermissionRationale
                        (context, WRITE_EXTERNAL_STORAGE)||
                        ActivityCompat.shouldShowRequestPermissionRationale
                                (context, CAMERA) ||
                        ActivityCompat.shouldShowRequestPermissionRationale
                                (context, READ_CONTACTS) || ActivityCompat.shouldShowRequestPermissionRationale
                        (context, CALL_PHONE) || ActivityCompat.shouldShowRequestPermissionRationale
                        (context, ACCESS_FINE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale
                        (context, READ_SMS)) {
                    Snackbar.make(findViewById(android.R.id.content),
                            "Please Grant Permissions",
                            Snackbar.LENGTH_INDEFINITE).setAction("ENABLE",
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ActivityCompat.requestPermissions(SpalshActivity.this,
                                            new String[]{READ_PHONE_STATE,WRITE_EXTERNAL_STORAGE,CAMERA, READ_CONTACTS, CALL_PHONE, ACCESS_FINE_LOCATION, READ_SMS},
                                            REQUEST_READ_PHONE_STATE);
                                }
                            }).show();
                } else {
                    ActivityCompat.requestPermissions(SpalshActivity.this,
                            new String[]{READ_PHONE_STATE,WRITE_EXTERNAL_STORAGE,CAMERA, READ_CONTACTS, CALL_PHONE, ACCESS_FINE_LOCATION, READ_SMS},
                            REQUEST_READ_PHONE_STATE);
                }
            }
            }

    }
Aristocrat answered 26/4, 2017 at 6:34 Comment(1)
Try yourActivity.this , instead of context.Leicester
M
10

First parameter is android.app.Activity type, You can't pass context at this place so use this instead of context like below code :-

if (ActivityCompat.shouldShowRequestPermissionRationale
                        (this, READ_PHONE_STATE) ||ActivityCompat.shouldShowRequestPermissionRationale
                        (this, WRITE_EXTERNAL_STORAGE)||
                        ActivityCompat.shouldShowRequestPermissionRationale
                                (this, CAMERA) ||
                        ActivityCompat.shouldShowRequestPermissionRationale
                                (this, READ_CONTACTS) || ActivityCompat.shouldShowRequestPermissionRationale
                        (this, CALL_PHONE) || ActivityCompat.shouldShowRequestPermissionRationale
                        (this, ACCESS_FINE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale
                        (this, READ_SMS))
Mapes answered 26/4, 2017 at 6:41 Comment(0)
C
0

Try replacing context with this

if (ActivityCompat.shouldShowRequestPermissionRationale(this, READ_PHONE_STATE) ||
    ActivityCompat.shouldShowRequestPermissionRationale(this, WRITE_EXTERNAL_STORAGE) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, CAMERA) ||  
    ActivityCompat.shouldShowRequestPermissionRationale(this, READ_CONTACTS) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, CALL_PHONE) ||  
    ActivityCompat.shouldShowRequestPermissionRationale(this, ACCESS_FINE_LOCATION) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, READ_SMS))  {
  //...
}
Chaechaeronea answered 27/4, 2017 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.