2013-06-27 188 views
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) 
     { 

     } 


    } 
+0

你收到後立即嘗試刪除該短信?這可能是一個計時問題 - 當您嘗試刪除最後一條SMS時,SMS數據庫可能尚未更新。如果不是這種情況,您可以嘗試其他方式刪除SMS,請參閱http://stackoverflow.com/questions/9389740/delete-an-sms-from-inbox –

回答

0

我一直以來在過去一小時在看這個,這是我發現迄今的東西,跳我幫助:)

void deleteMessage(Context context){ 
    Uri uriSms = Uri.parse("content://sms/inbox"); 
    Cursor c = context.getContentResolver().query(uriSms, null,null,null,null); 
    int id = c.getInt(0); 
    int thread_id = c.getInt(1); //get the thread_id 
    context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null); 
} 

void deleteSMS(Context context){ 
    Uri deleteUri = Uri.parse("content://sms"); 
    context.getContentResolver().delete(deleteUri, "address=? and date=?", new String[] {strMessageFrom,strTimeStamp}); 
} 

public void deleteSMS1(Context context, String message, String number) { 
    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); 
       Log.e("log>>>", 
         "0>" + c.getString(0) + "1>" + c.getString(1) 
           + "2>" + c.getString(2) + "<-1>" 
           + c.getString(3) + "4>" + c.getString(4) 
           + "5>" + c.getString(5)); 
       Log.e("log>>>", "date" + c.getString(0)); 

       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[] { c.getString(4) }); 
        Log.e("log>>>", "Delete success........."); 
       } 
      } while (c.moveToNext()); 
     } 
    } catch (Exception e) { 
     Log.e("log>>>", e.toString()); 
    } 
} 
相關問題