Skip to content

Instantly share code, notes, and snippets.

@TheRyanMiller
Last active June 1, 2016 01:19
Show Gist options
  • Save TheRyanMiller/a07b3de579667257be4545551c925f59 to your computer and use it in GitHub Desktop.
Save TheRyanMiller/a07b3de579667257be4545551c925f59 to your computer and use it in GitHub Desktop.
Requesting Permissions
private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 1; //Custom value identifier for permission req
private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 2;
if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED){
//Permission is not granted
}
else{
success = sendSms(msg, smsRecipient);
if(success==false) {
Toast.makeText(MainActivity.this,"Need to enter a valid ph# & text before you can send",Toast.LENGTH_SHORT).show();
}
else {
((EditText) findViewById(R.id.editText)).setText("");
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permissions are obtained. Execute the following actions...
//Toast.makeText(MainActivity.this,"Permissions are now obtained",Toast.LENGTH_SHORT).show();
return;
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
}
//Additional cases if multiple permissions are possible
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment