user3181843
user3181843

Reputation: 185

Runtime permission with 2 request in the same activity

I'm trying to ask for 2 runtime permissions int he same activity. The first one will trigger by button and ask for READ_CONTACTS, if allowed will go forward via onActivityResult. The other runtime permission will ask SEND_SMS and if allowed will go to onRequestPermissionsResult. Now, as I understand how it workd - I'm doing it wrong. The 2 runtime permissions need to run through one of this choices and I need to build a method with Switch Case or something to handle the 2 permissions so it won't crash. I need idea how to handle this 2 events so I won't allow the contacts permission and the activity will try to run the SMS method.

That's the onActivityResult override that I can't get rid of.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) {
            Log.d(TAG, "Response: " + data.toString());
            uriContact = data.getData();

            retrieveContactNumber();
        }
    }
    catch (Exception e){
        e.getStackTrace();
        Toast.makeText(SmsActivity.this, "Hi, I Tried :P", Toast.LENGTH_SHORT).show();
    }
}



private void retrieveContactNumber() {

    Cursor cursorID = getContentResolver().query(uriContact, new String[]{ContactsContract.Contacts._ID}, null, null, null);

    if (cursorID.moveToFirst()) {

        contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
    }

    cursorID.close();

    Log.d(TAG, "Contact ID: " + contactID);


    Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                    ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                    ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

            new String[]{contactID},
            null);

    if (cursorPhone.moveToFirst()) {
        contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }

    cursorPhone.close();
    txtCbNum = (EditText) findViewById(R.id.txtNum);
    txtCbNum.setText(contactNumber, TextView.BufferType.EDITABLE);

    Log.d(TAG, "Contact Phone Number: " + contactNumber);
}


private void requestSmsPermission() {

    if (ContextCompat.checkSelfPermission(SmsActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(SmsActivity.this,
                new String[]{Manifest.permission.SEND_SMS},
                PERMISSION_SEND_SMS);
    } else {
        contactNumber = txtCbNum.getText().toString();
        sendSms(contactNumber, dibur);
    }


}

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

    if (requestCode == PERMISSION_SEND_SMS) {

        contactNumber = txtCbNum.getText().toString();

        sendSms(contactNumber, dibur);
    } else if (requestCode == MY_PERMISSIONS_REQUEST_READ_CONTACTS) {
        try {
            // need to insert the contact number retrieve here

        } catch (Exception e) {
            e.getStackTrace();
            Toast.makeText(SmsActivity.this, "Hi, I Tried :P", Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(SmsActivity.this, "Permission denied, Can't send SMS.", Toast.LENGTH_LONG).show();
    }

}


private void sendSms(String phoneNumber, String message) {
    try {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
        Toast.makeText(SmsActivity.this, "Sms sent successfully to " + contactNumber, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(SmsActivity.this, "SMS failed, please try again." + contactNumber, Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

Upvotes: 0

Views: 2845

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007399

You can tell your two requestPermissions() calls apart by the integer that you are passing as the second parameter. That value is supplied to onRequestPermissionsResult() as a parameter.

So, for example, if in one place you have:

ActivityCompat.requestPermissions(this, ..., RESULT_PERMS_RECORD_VIDEO);

(with an array of permission names for ...)

and in another place you have:

ActivityCompat.requestPermissions(this, ..., RESULT_PERMS_TAKE_PICTURE);

(with, presumably, a different array of permission names for ...)

then you can have:

  @Override
  public void onRequestPermissionsResult(int requestCode,
                                         String[] permissions,
                                         int[] grantResults) {
    if (requestCode==RESULT_PERMS_TAKE_PICTURE) {
      // do something
    }
    else if (requestCode==RESULT_PERMS_RECORD_VIDEO) {
      // do something else
    }
  }

Upvotes: 1

Related Questions