Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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 = newHashSet<>();
phonesSet.add(phoneNumber);
longthreadId= Threads.getOrCreateThreadId(context, phonesSet); // get the thread-id of the specific conversation threadUrithreadUri= ContentUris.withAppendedId(Threads.CONTENT_URI, threadId); // get the thread-uri

String[] projection = newString[]{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};

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

Solution 2:

This is the full code solution of being able to obtain the Sent/Received SMS messages on various android devices. This has been tested on API level 22, 23, 26 and 28 on different android devices including Huawei, Oppo and Samsung.

publicvoidgetAllSms(Context context)
{
    HashSet<String> phoneSet = newHashSet<>();
    phoneSet.add(SelectedPhNo);  // phoneNumberlongthreadId= Telephony.Threads.getOrCreateThreadId(context, phoneSet);
    UrithreadUri= ContentUris.withAppendedId(Telephony.Threads.CONTENT_URI, threadId);

    String[] projection = newString[] {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, Telephony.Sms.LOCKED,
            Telephony.Sms.ERROR_CODE, Telephony.Sms.SEEN, Telephony.Sms.Inbox.BODY, Telephony.Sms.Sent.BODY};

    Cursorcur= context.getContentResolver().query(threadUri, projection, null, null, "normalized_date desc"); 
    DatabaseUtils.dumpCursor(cur);

    // Read cursor into an arraylist
    ArrayList<String> mArrayList = newArrayList<String>();

    inttotalSms= cur.getCount();

    if(cur.moveToFirst())
    {
         for(inti=0; i < totalSms; i++)
         {
              Stringbody= cur.getString(cur.getColumnIndex(Telephony.Sms.BODY));
              StringindexDate= cur.getString(cur.getColumnIndex(Telephony.Sms.DATE));

              // Convert string to long variableLongdate= Long.parseLong(indexDate);

              // Convert millis value to proper formatDatedateVal=newDate(date);

              //"dd-MMM-yyyy""dd/MM/yyyy"SimpleDateFormatformat=newSimpleDateFormat("HH:mm:ss  dd-MM-yyyy");
              dateText = format.format(dateVal);

              cur.moveToNext();

              inboxArrayAdapter.add("Command: " + body + "\n" + "Date: " + dateText);
         }
    }
}

Post a Comment for "Sms Messages Of A Particular Number Not Showing Up On Other Devices Android"