I have been using a cursor to load in sent and received sms messages for a particular phone number that has been selected by the user.

        // Number needs to saved in +614 format
        String phoneNumber = SelectedPhNo;
        String sms = "address='"+ phoneNumber + "'";

        ContentResolver cr = context.getContentResolver();

        Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null , sms , null , null); 

        // Where I have sms in the code is where we filter the listview for that particular number 

For API 22 running on a Huawei phone, the cursor is working completely fine returning the total amount of sms messages and displaying the sent/received items in a listview. In the android studio debugger where I am stepping into the code, the variable totalSms is showing the correct value of total sms messages.

When the cursor is being tested on a Samsung S9 running on API 28, the cursor getCount() is showing as zero in the Android Studio debugger and not showing anything in the listview, even though there are sms messages on the phone. This is happening on the new API versions.

If I don't set a selection filter in the cursor query, it will show all the SMS sent/received messages on the phone.

Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null , null , null , null);

I can boil it down to maybe it is this line below, which is causing the filter to not work on the newer Android API versions.

String sms = "address='"+ phoneNumber + "'";

Full code attempted solution

 @WithPermissions(permissions = {Manifest.permission.READ_SMS})
    @TargetApi(Build.VERSION_CODES.M)
    public void getAllSms(Context context)
    {
        // Number needs to saved in +614 format
        String phoneNumber = SelectedPhNo;
        String sms = "address='"+ phoneNumber + "'";

        ContentResolver cr = context.getContentResolver();
        Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null , sms , null , null); 

        int totalSms = 0;

        String type = null;
        if(c != null)
        {
            totalSms = c.getCount();
            totalSms = totalSms;

            if(c.moveToFirst())
            {
                for(int j = 0; j < totalSms; j++)
                {
                    String smsDate = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.DATE));
                    String body = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.BODY));

                    switch(Integer.parseInt(c.getString(c.getColumnIndexOrThrow(Telephony.Sms.TYPE))))
                    {
                        case Telephony.Sms.MESSAGE_TYPE_INBOX:
                            type = "inbox";
                            break;
                        case Telephony.Sms.MESSAGE_TYPE_SENT:
                            type = "sent";
                            break;
                        case Telephony.Sms.MESSAGE_TYPE_OUTBOX:
                            type = "outbox";
                            break;
                        default:
                            break;
                    }

                    // Convert smsDate to readable format
                    Long date = Long.parseLong(smsDate);

                    // Convert millis value to proper format
                    Date dateVal = new Date(date);

                    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
                    dateText = format.format(dateVal);

                    //Toast.makeText(context, "Message present", Toast.LENGTH_SHORT).show();
                    inboxArrayAdapter.add("Command: " + body + "\n" + "Date: "+ dateText);

                    // Iterate through the list of SMS messages to be displayed in the listview
                    c.moveToNext();

                    //  Update listview as soon as we receive a new message
                    ((BaseAdapter)inboxmessages.getAdapter()).notifyDataSetChanged();

                    inboxArrayAdapter.notifyDataSetChanged();

                    Log.e("MyActivity", body);
                }
            }
        }
        else
        {
            Toast.makeText(getContext(), "No Messages found for this contact!", Toast.LENGTH_SHORT).show();
        }
}

0 Answers