Asked  7 Months ago    Answers:  5   Viewed   67 times

I know that Android 6.0 has new permissions and I know I can call them with something like this

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

Today I saw a Google app which needs 3 permissions: contacts, sms and camera. It's making a page 1-3 and calls them all together at the same time to activate.

Can anybody tell me how I can call 4 permissions to activate at the same time like sms, camera, contacts and storage?

Example (forgot the name of the google app :( )
The app needs sms,contacts and camera

the app asked me (and made a dialog page1-3) activate sms, activate contacts and then camera. So this google app was calling all 3 required permissions together and my question is how can i achive the same ?

 Answers

16

Just include all 4 permissions in the ActivityCompat.requestPermissions(...) call and Android will automatically page them together like you mentioned.

I have a helper method to check multiple permissions and see if any of them are not granted.

public static boolean hasPermissions(Context context, String... permissions) {
    if (context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

Or in Kotlin:

fun hasPermissions(context: Context, vararg permissions: String): Boolean = permissions.all {
    ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}

Then just send it all of the permissions. Android will ask only for the ones it needs.

// The request code used in ActivityCompat.requestPermissions()
// and returned in the Activity's onRequestPermissionsResult()
int PERMISSION_ALL = 1; 
String[] PERMISSIONS = {
  android.Manifest.permission.READ_CONTACTS, 
  android.Manifest.permission.WRITE_CONTACTS, 
  android.Manifest.permission.WRITE_EXTERNAL_STORAGE, 
  android.Manifest.permission.READ_SMS, 
  android.Manifest.permission.CAMERA
};

if (!hasPermissions(this, PERMISSIONS)) {
    ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
Tuesday, June 1, 2021
 
AlterPHP
answered 7 Months ago
15

What is "MY_PERMISSIONS_REQUEST_READ_CONTACTS" in this example?

It is an int, to tie a particular requestPermissions() call to the corresponding onRequestPermissionsResult() callback.

Under the covers, requestPermissions() uses startActivityForResult(); this int serves the same role as it does in startActivityForResult().

does that mean I should make a Constants.java and declare a public static int?

I would just make it a private static final int in the activity. But, you can declare it wherever you want.

What should the value be?

I seem to recall that it needs to be below 0x8000000, but otherwise it can be whatever you want. The value that you use for each requestPermissions() call in an activity should get a distinct int, but the actual numbers do not matter.

If your activity has only one requestPermissions() call, then the int value really does not matter. But many apps will have several requestPermissions() calls in an activity. In that case, the developer may need to know, in onRequestPermissionsResult(), what request this is the result for.

Monday, June 14, 2021
 
LukeP
answered 6 Months ago
13

An example of implementing Permissions in any Activity.

SampleActivity.java

public class SampleActivity extends AppCompatActivity{
    private final int PERMISSION_CODE = 1;
    Button button;
    @override
    onCreate(Bundle savedInstance){
        super.onCreate(savedInstance);
        setContentView(R.layout.your_layout);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener(){
            @override
            public void onClick(View view){
                requestPermissionAndContinue();
            }
        });
        //remaining code to continue using the app
        //your actual code should also be in this same class
    }

    private void requestPermissionAndContinue(){
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
            if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)){
                Log.e(TAG, "permission denied, show dialog");
            }else{
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, PERMISSION_CODE);
            }
        }else{
            accessContacts();
        }
    }

    private void accessContacts(){
        //your code once you receive permission
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(grantResults.length > 0 && permissions[0]==PackageManager.PERMISSION_GRANTED){
            accessContacts();
        }else{
             //redirect to settings page or ask permission again
        }
    }
}
Friday, August 13, 2021
 
bsd
answered 4 Months ago
bsd
89

Make a readwrite permission. Each view gets one and only one permission but each principal can be mapped to many permissions.

Sunday, October 31, 2021
 
LaTeX
answered 1 Month ago
68

Hence I understand (I might be wrong) it is not possible to have more than one permission to be mentioned here

Correct.

But there seems to be no room to specify this in the AndroidManifest.xml.

Correct.

I have a requirement to parse the AndroidManifest.xml of an app to find out what permissions a client app need to possess in order to communicate with this app

Technically, this is not possible, as apps can check for permissions at runtime (e.g., by calling checkCallingPermission()).

Sunday, November 21, 2021
 
pawello2222
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share