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

做~自己de王妃 提交于 2019-11-29 18:49:32

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);
Sahil Bora

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.

public void getAllSms(Context context)
{
    HashSet<String> phoneSet = new HashSet<>();
    phoneSet.add(SelectedPhNo);  // phoneNumber
    long threadId = Telephony.Threads.getOrCreateThreadId(context, phoneSet);
    Uri threadUri = ContentUris.withAppendedId(Telephony.Threads.CONTENT_URI, threadId);

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

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

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

    int totalSms = cur.getCount();

    if(cur.moveToFirst())
    {
         for(int i = 0; i < totalSms; i++)
         {
              String body = cur.getString(cur.getColumnIndex(Telephony.Sms.BODY));
              String indexDate = cur.getString(cur.getColumnIndex(Telephony.Sms.DATE));

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

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

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

              cur.moveToNext();

              inboxArrayAdapter.add("Command: " + body + "\n" + "Date: " + dateText);
         }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!