在Android程序中,是否有可能通过编程方式检测到消息已从消息存储中删除?如果是,我们是否可以访问已删除的短信信息。
content://mms-sms
)上注册一个ContentObserver
,每当发生更改时,您将获得回调。请注意,您需要扫描 ContentProvider
并保存其当前状态,然后每次有更改时,您都需要搜索 ContentProvider
以确定发生了什么变化:没有预先打包的方法可以通知您用户删除了特定消息。只需使用此代码
try {
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(
uriSms,
new String[] { "_id", "thread_id", "address", "person",
"date", "body" }, "read=0", null, null);
if (c != null && c.moveToFirst()) {
do {
long id = c.getLong(0);
long threadId = c.getLong(1);
String address = c.getString(2);
String body = c.getString(5);
String date = c.getString(3);
if (message.equals(body) && address.equals(number)) {
// mLogger.logInfo("Deleting SMS with id: " + threadId);
context.getContentResolver().delete(
Uri.parse("content://sms/" + id), "date=?",
new String[] { <your date>});
Log.e("log>>>", "Delete success.........");
}
} while (c.moveToNext());
}
} catch (Exception e) {
Log.e("log>>>", e.toString());
}