使用新的电话内容提供商读短信 [英] Using new Telephony content provider to read SMS

查看:146
本文介绍了使用新的电话内容提供商读短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照 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())
}

请注意:我知道如何使用<一个href="http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-inbox-programmatically-in-android">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;
}

这篇关于使用新的电话内容提供商读短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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