public void SendSMS(View view) { int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS); if (permissionCheck == PackageManager.PERMISSION_GRANTED) { } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, REQUEST_SEND_PHONE_SMS); } int permissionCheck2 = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE); if (permissionCheck == PackageManager.PERMISSION_GRANTED) { // if both are true then do tshi } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_SEND_PHONE_SMS) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (requestCode == REQUEST_READ_PHONE_STATE) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // MyMessage(); Toast.makeText(this, "Both OK", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "You don't have required permission to send a message", Toast.LENGTH_SHORT).show(); return; } } else { Toast.makeText(this, "You don't have required permission to send a message", Toast.LENGTH_SHORT).show(); return; } } } }
if (requestCode == REQUEST_SEND_PHONE_SMS) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (requestCode == REQUEST_READ_PHONE_STATE) { ...
requestCode
public void SendSMS() { String[] sPermissions = { Manifest.permission.SEND_SMS, Manifest.permission.READ_PHONE_STATE }; if (! hasPermissions(sPermissions)) androidx.core.app.ActivityCompat.requestPermissions(this, sPermissions, 1); } public boolean hasPermissions( String... sPermissions ) { for (String sPermission : sPermissions) { if (checkSelfPermission(sPermission) != PackageManager.PERMISSION_GRANTED) return false; } return true; } public void onRequestPermissionsResult( int requestCode, @androidx.annotation.NonNull String[] permissions, @androidx.annotation.NonNull int[] grantResults ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 1) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) Toast.makeText(this, "Permissions OK to now send message", Toast.LENGTH_SHORT).show(); else Toast.makeText(this, "You don't have required permission(s) to send a message", Toast.LENGTH_SHORT).show(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)