Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.1 kB
2
Indexable
Never
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
        != PackageManager.PERMISSION_GRANTED) {
    // Permission is not granted
    // You should request the permission
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.READ_SMS},
            MY_PERMISSIONS_REQUEST_READ_SMS);
} else {
    // Permission has already been granted
    // Proceed with reading SMS
    readSMS();
}



@Override
public void onRequestPermissionsResult(int requestCode,
        @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == MY_PERMISSIONS_REQUEST_READ_SMS) {
        // If request is cancelled, the result arrays are empty
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission was granted, proceed with reading SMS
            readSMS();
        } else {
            // Permission denied, handle accordingly
            Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
        }
    }
}
Leave a Comment