根据4.4 SMS APIs,新版本提供了以下功能:
允许应用在设备上读取和写入短信和彩信
我无法找到有关此功能的任何信息,也无法找到新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 Content Provider,但不支持此方法。根据链接的API,我应该能够以受支持的方式执行此操作。
答案 0 :(得分:30)
看起来您可以使用此类来使其正常工作。该套餐为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;
}
答案 1 :(得分:1)
我是这样做的。创建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-Objects
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;
}
答案 2 :(得分:0)
查询当前默认的SMS应用程序包名称并保存。
String defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(context);
请求用户将默认短信应用程序更改为您的应用程序以恢复短信(您必须是默认短信应用才能写入短信提供商)。
Intent intent = new Intent(context, Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName());
startActivity(intent);