在清单文件中添加权限,例如
<uses-permission android:name="android.permission.RECEIVE_SMS"android:protectionLevel="signature" />
<uses-permission android:name="android.permission.READ_SMS" />
然后添加意图过滤器:
<receiver android:name="com.aquadeals.seller.services.SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
获取所有短信:
TelephonyProvider telephonyProvider = new TelephonyProvider(context);
List<Sms> smses = telephonyProvider.getSms(Filter.ALL).getList();
删除短信:
通过将读取短信正文的值存储到共享首选项并仅删除共享首选项中的内容来实现此目的。
Uri inboxUri = Uri.parse("content://sms/inbox");
int count = 0;
Cursor c = getContentResolver().query(inboxUri , null, null, null, null);
while (c.moveToNext()) {
try {
// Delete the SMS
String pid = c.getString(0); // Get id;
String uri = "content://sms/" + pid;
// uri.
count =getContentResolver().delete(Uri.parse(uri),
null, null);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
所有 Android 内容提供商如:联系人、通话记录、日历……包含所有选项的完整文档 https://web.archive.org/web/20121022021217/http://mobdev.olin.edu/mobdevwiki/FrontPage/Tutorials/SMS%20Messaging