特定号码的短信未在其他设备上显示 [英] SMS Messages of a particular number not showing up on other devices Android

本文介绍了特定号码的短信未在其他设备上显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Telephony.Sms库加载正在处理的应用程序的已接收和已发送的SMS消息.当我将查询选择设置为null(查询中的第三项)时,它将在我测试过的不同类型的电话上显示所有已发送和已接收的短信.

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

但是当我将其设置为特定数字时,在运行于API 27的Samsung S9手机上,它没有显示任何短信.在API 23上运行的Nexus上,它将在列表视图中显示已接收的消息,但不显示已发送的消息.在运行API 22的华为手机上,所有手机均正常运行,并显示特定编号的已发送和已接收消息.

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 + "\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();;
                }
            }
        }
        else
        {
            Toast.makeText(getContext(), "No Messages found for this contact!", Toast.LENGTH_SHORT).show();
        }
    }

推荐答案

这是能够在各种android设备上获取已发送/已接收SMS消息的完整代码解决方案.已在不同的Android设备(包括华为,Oppo和三星)的API级别22、23、26和28上对此进行了测试.

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);
         }
    }
}

这篇关于特定号码的短信未在其他设备上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆