Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

SMS Messages of a particular number not showing up on other devices Android

I am using the Telephony.Sms library to load in received and sent sms messages for the app I am working on. When I set the query selection to null (the third item in the query), it will show all the sent and received sms messages on the different types of phones I have been testing on.

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

But when I set it to a particular number, on the Samsung S9 phone running on API 27 it is not showing any sms messages. On the Nexus runnning on API 23, it will show the received messages but not the sent messages in the listview. On the Huawei phone running on API 22, it's all working properly, showing the sent and received messages of the particular number.

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

Here's the full code retrieving the sent and received sms messages for a particular phone number.

@WithPermissions(permissions = {Manifest.permission.READ_SMS})
    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 , null , null , null);  // Sms not showing up on Raza's phone
        int totalSms = 0;


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

            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 + "
" + "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();;
                }
            }
        }
        else
        {
            Toast.makeText(getContext(), "No Messages found for this contact!", Toast.LENGTH_SHORT).show();
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Querying for SMS/MMS messages is very tricky, and varies a lot between different Android versions and between different makers.

This is the version that should work properly on all Android K+ devices:

HashSet<String> phonesSet = new HashSet<>();
phonesSet.add(phoneNumber);
long threadId = Threads.getOrCreateThreadId(context, phonesSet); // get the thread-id of the specific conversation thread
Uri threadUri = ContentUris.withAppendedId(Threads.CONTENT_URI, threadId); // get the thread-uri

String[] projection = new String[]{MmsSms.TYPE_DISCRIMINATOR_COLUMN, BaseColumns._ID, Conversations.THREAD_ID,
                    Telephony.Sms.ADDRESS, Telephony.Sms.BODY, "sort_index", Telephony.Sms.DATE_SENT,
                    Telephony.Sms.READ, Telephony.Sms.TYPE, Telephony.Sms.STATUS, Telephony.Sms.LOCKED,
                    Telephony.Sms.ERROR_CODE, Telephony.Sms.SEEN};

Cursor cur = getContentResolver().query(threadUri, projection, null, null, null);
DatabaseUtils.dumpCursor(cur);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...