I have a function that reads text messages from a specific contact and displays them in a list. This worked perfectly on older Android versions but stopped working on API 29.
The loadTextMessages(getContext()) method gets completely ignored when running on API 29. I can even set a debug breakpoint right where it should execute, but it never gets hit. However, the same code runs fine on API 22.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_messages, container, false);
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission not granted
} else {
// This function call is being skipped on API 29
loadTextMessages(getContext());
}
return rootView;
}
@WithPermissions(permissions = {Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS})
@TargetApi(Build.VERSION_CODES.M)
public void loadTextMessages(Context ctx) {
HashSet<String> contactSet = new HashSet<>();
contactSet.add(selectedContact);
long conversationId = Telephony.Threads.getOrCreateThreadId(ctx, contactSet);
Uri conversationUri = ContentUris.withAppendedId(Telephony.Threads.CONTENT_URI, conversationId);
String[] columns = new String[] {Telephony.MmsSms.TYPE_DISCRIMINATOR_COLUMN, BaseColumns._ID,
Telephony.Sms.Conversations.THREAD_ID, Telephony.Sms.ADDRESS, Telephony.Sms.BODY,
"sort_index", Telephony.Sms.DATE_SENT, Telephony.Sms.DATE, Telephony.Sms.READ,
Telephony.Sms.TYPE, Telephony.Sms.STATUS};
Cursor cursor = ctx.getContentResolver().query(conversationUri, columns, null, null, "normalized_date desc");
DatabaseUtils.dumpCursor(cursor);
ArrayList<String> messageList = new ArrayList<String>();
int messageCount = cursor.getCount();
if(cursor.moveToFirst()) {
for(int j = 0; j < messageCount; j++) {
String messageText = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
String timestamp = cursor.getString(cursor.getColumnIndex(Telephony.Sms.DATE));
Long timeValue = Long.parseLong(timestamp);
Date messageDate = new Date(timeValue);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
String formattedDate = formatter.format(messageDate);
cursor.moveToNext();
messageAdapter.add("Text: " + messageText + "\n" + "Time: " + formattedDate);
}
}
}
What could cause this behavior change between Android versions?