使用新的电话内容提供商读短信提供商、短信、电话、内容

2023-09-12 11:19:27 作者:不离不弃纯属放P

按照 4.4手机短信的API 中,新版本提供的功能为:

According to the 4.4 SMS APIs, the new version provides functionality to:

允许应用程序读写设备上的短信和彩信

allow apps to read and write SMS and MMS messages on the device

我无法找到此功能的任何信息,也没有在新的SDK任何样品。这是我迄今为止阅读新收到的邮件。

I can't find any information on this functionality, nor any samples in the new SDK. This is what I have so far for reading new incoming messages.

不过,我想为读取现有的消息存储在deivce:

However, I would like to read existing messages stored on the deivce:

// Can I only listen for incoming SMS, or can I read existing stored SMS?
SmsMessage[] smsList = Telephony.Sms.Intents.getMessagesFromIntent(intent);
for(SmsMessage sms : smsList) {
    Log.d("Test", sms.getMessageBody())
}

请注意:我知道如何使用SMS内容提供商,但这种方法是不支持的。根据连锁的API,我应该能够做到这一点的支持方式。

Please note: I know about using the SMS Content Provider, however this method is unsupported. According to the linked APIs, I should be able to do this in a supported way.

推荐答案

看起来你将能够使用这个类来获得它的工作。该软件包是 Telephony.Sms.Conversations 。

It looks like you would be able to use this class to get it working. The package is Telephony.Sms.Conversations.

尽管下面的code使用内容提供商的方法,这是现在用于读取短信的API等级19(奇巧)增加了一个官方的API。

Although the following code uses the content provider method, this is now an official API added in API Level 19 (KitKat) for reading the SMS messages.

public List<String> getAllSmsFromProvider() {
  List<String> lstSms = new ArrayList<String>();
  ContentResolver cr = mActivity.getContentResolver();

  Cursor c = cr.query(Telephony.Sms.Inbox.CONTENT_URI, // Official CONTENT_URI from docs
                      new String[] { Telephony.Sms.Inbox.BODY }, // Select body text
                      null,
                      null,
                      Telephony.Sms.Inbox.DEFAULT_SORT_ORDER); // Default sort order

  int totalSMS = c.getCount();

  if (c.moveToFirst()) {
      for (int i = 0; i < totalSMS; i++) {
          lstSms.add(c.getString(0));
          c.moveToNext();
      }
  } else {
      throw new RuntimeException("You have no SMS in Inbox"); 
  }
  c.close();

  return lstSms;
}