I am trying to read SMS from my android device. I have written the required code however, I am getting permission denial error. I have added User-Permission (READ-SMS) in Android Manifest file. I am getting out of permission error, I am printing it in the Log.i (else block)
My code is given below:
public ArrayList fetchInbox()
{
ArrayList sms = new ArrayList();
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null);
if(ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) { Cursor cursor = cr.query(uriSms, new String[]{"_id", "address", "date", "body", "read"}, "address = '+2345679'", null, null);
getSmsLogs(cursor, MainActivity.this);
Log.i("Hello", String.valueOf(cursor));
cursor.moveToFirst();
while (cursor.moveToNext()) {
String address = cursor.getString(1);
String body = cursor.getString(3);
Log.i("Hello","======> Mobile number => " + address);
Log.i("Hello","=====> SMS Text => " + body);
sms.add("Address=> " + address + "n SMS => " + body);
}
}
else
{
Log.i("Hello", "Out of permission");
}
return sms;
}
The error message :
java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=28309, uid=10060 requires android.permission.READ_SMS, or grantUriPermission()
TargetSdkVersion : 23
Thank you.