(转)[Android实例] 关于使用ContentObserver监听不到删除短信会话的解决方案
最近做通讯录的项目,需要实时监听短信的删除,就用到了观察者ContentObserver,怪异的事情就此发生,当我删除一条短信的时候,可以监听到,但是,当我删除整条短信的时候,就无法监听到,查了很多资料,都没有一个好的解决方案,顺便吐糟一下:大家来eoe发问题,问题解决了,也不说把解决的方法给大家分享一下。。。
OK,先看源码吧
com.android.providers.telephony.MmsSmsProvider这个类的删除操作源码
@Override
public int delete(Uri uri, String selection,
String[] selectionArgs) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Context context = getContext();
int affectedRows = 0;
switch(URI_MATCHER.match(uri)) {
case URI_CONVERSATIONS_MESSAGES:
long threadId;
try {
threadId = Long.parseLong(uri.getLastPathSegment());
} catch (NumberFormatException e) {
Log.e(LOG_TAG, "Thread ID must be a long.");
break;
}
affectedRows = deleteConversation(uri, selection, selectionArgs);
MmsSmsDatabaseHelper.updateThread(db, threadId);
break;
case URI_CONVERSATIONS:
affectedRows = MmsProvider.deleteMessages(context, db,
selection, selectionArgs, uri)
+ db.delete("sms", selection, selectionArgs);
// Intentionally don't pass the selection variable to updateAllThreads.
// When we pass in "locked=0" there, the thread will get excluded from
// the selection and not get updated.
MmsSmsDatabaseHelper.updateAllThreads(db, null, null);
break;
case URI_OBSOLETE_THREADS:
affectedRows = db.delete("threads",
"_id NOT IN (SELECT DISTINCT thread_id FROM sms " +
"UNION SELECT DISTINCT thread_id FROM pdu)", null);
break;
default:
throw new UnsupportedOperationException(NO_DELETES_INSERTS_OR_UPDATES);
}
if (affectedRows > 0) {
context.getContentResolver().notifyChange(MmsSms.CONTENT_URI, null);
}
return affectedRows;
}
注意红字部分,当删除整条会话的时候,系统发的通知是MmsSms.CONTENT_URI,查证:MmsSms.CONTENT_URI=Uri.parse("content://mms-sms")
OK~问题就此解决
我可以说我找的蛋都碎了吗,我可以说大家能不能有点互帮互助的精神,拜托,大家都是敲代码的。。。
http://www.eoeandroid.com/thread-325408-1-1.html
http://www.eoeandroid.com/thread-325255-1-1.html