繁体   English   中英

使用新的Telephony内容提供商来阅读SMS

[英]Using new Telephony content provider to read SMS

根据4.4 SMS API ,新版本提供以下功能:

允许应用在设备上读取和写入短信和彩信

我找不到有关此功能的任何信息,也无法找到新SDK中的任何示例。 这是我到目前为止读取新传入消息的方法。

但是,我想阅读存储在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,我应该能够以受支持的方式执行此操作。

看起来您可以使用此类来使其正常工作。 该软件包是Telephony.Sms.Conversations

虽然以下代码使用内容提供程序方法,但现在这是API级别19(KitKat)中添加的用于读取SMS消息的官方API。

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

我这样做了以下。 创建SMSObject:

public class SMSObject {
  private String _id;
  private String _address;
  private String _msg;
  private String _readState; // "0" for have not read sms and "1" for have
                            // read sms
  private String _time;
  private String _folderName;

  //+ getter and setter methods and 

  @Override
  public String toString() {
    return "SMSObject [_id=" + _id + ", _address=" + _address + ", _msg="
            + _msg + ", _readState=" + _readState + ", _time=" + _time
            + ", _folderName=" + _folderName + "]";
}

这里有一个简单的函数,它只记录所有当前的SMS对象

private void readSMS() {
    List<SMSObject> lstSms = new ArrayList<SMSObject>();
    SMSObject objSms = new SMSObject();
    Uri message = Uri.parse("content://sms/");
    ContentResolver cr = this.getContentResolver();

    Cursor c = cr.query(message, null, null, null, null);
    // this.startManagingCursor(c);
    int totalSMS = c.getCount();
    Log.d("SMS Count->", "" + totalSMS);
    if (c.moveToFirst()) {
        for (int i = 0; i < totalSMS; i++) {

            objSms = new SMSObject();
            objSms.setId(c.getString(c.getColumnIndexOrThrow("_id")));
            objSms.setAddress(c.getString(c
                    .getColumnIndexOrThrow("address")));
            objSms.setMsg(c.getString(c.getColumnIndexOrThrow("body")));
            objSms.setReadState(c.getString(c.getColumnIndex("read")));
            objSms.setTime(c.getString(c.getColumnIndexOrThrow("date")));
            if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
                objSms.setFolderName("inbox");
            } else {
                objSms.setFolderName("sent");
            }

            lstSms.add(objSms);

            Log.d("SMS at " + i, objSms.toString());

            c.moveToNext();
        }
    }
    // else {
    // throw new RuntimeException("You have no SMS");
    // }
    c.close();

    // return lstSms;

}

前几天我发现了这个,记不起来自哪个网站; 如果用户已选择将应用程序设为默认短信应用程序,则只能还原消息。 这可能会也可能不会完全回答您的问题。 我还没试过这个

  1. 查询当前默认的SMS应用程序包名称并保存。

     String defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(context); 
  2. 请求用户将默认SMS应用程序更改为您的应用程序以恢复SMS消息(您必须是默认的SMS应用程序才能写入SMS提供程序)。

     Intent intent = new Intent(context, Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName()); startActivity(intent); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM