3
我正在開發一個Android應用程序,我想用一些新文本替換上次收到的短信正文。如何從Android中的收件箱中刪除最近收到的短信
我正在使用BroadcastReceiver
其中我想將最後收到的SMS的消息正文存儲在變量中,並從收件箱中刪除SMS,現在刪除後我希望在收件箱中放入新的編碼消息。
現在我面臨的問題是如何從收件箱中刪除上次收到的短信。我已經在這方面開發了一些代碼,但它從收件箱中刪除了second last
(之前)的SMS。請檢查我的代碼,並幫助我繼續我的應用程序,我會非常感謝你的這種善舉。
public void deleteLastSMS()
{
// abortBroadcast();
String body = null;
String num = null;
try
{
Uri uri = Uri.parse("content://sms/inbox");
Cursor c =contex.getContentResolver().query(uri, null, null ,null,null);
if(c.moveToFirst())
{
body = c.getString(c.getColumnIndexOrThrow("body")).toString();
num = c.getString(c.getColumnIndexOrThrow("address")).toString();
}
int id = c.getInt(0);
int thread_id = c.getInt(1);
Uri thread = Uri.parse("content://sms");
contex.getContentResolver().delete(thread, "thread_id=? and _id=?", new String[]{String.valueOf(thread_id), String.valueOf(id)});
}
catch(CursorIndexOutOfBoundsException ee)
{
}
}
你收到後立即嘗試刪除該短信?這可能是一個計時問題 - 當您嘗試刪除最後一條SMS時,SMS數據庫可能尚未更新。如果不是這種情況,您可以嘗試其他方式刪除SMS,請參閱http://stackoverflow.com/questions/9389740/delete-an-sms-from-inbox –