2012-02-22 129 views
8

一旦用戶閱讀,我想從收件箱中刪除短信。這個怎麼做?從收件箱中刪除短信

編輯:

公共類SmsReceiver擴展廣播接收器{

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 

    Bundle bundle = intent.getExtras();   
    SmsMessage[] msgs = null; 
    String address = null; 

    if(bundle!=null) { 
     String info = " "; 
     Object[] pdus = (Object[]) bundle.get("pdus"); 
     msgs = new SmsMessage[pdus.length]; 

     for (int i=0; i<msgs.length; i++) { 
      msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);     
       address=msgs[i].getDisplayOriginatingAddress(); 
       info += msgs[i].getMessageBody().toString(); 

     } 
     /*String str=bundle.getString("state"); 
     Log.v("State",str);*/ 

     if((PhoneNumberUtils.isWellFormedSmsAddress(address))){    //set ! and address length 

     //abortBroadcast(); 
     Log.v("phone num","wellformed"); 
      Uri deleteUri = Uri.parse("content://sms"); 

      Cursor c = context.getContentResolver().query(deleteUri, null, null, 
        null, null); 
      while (c.moveToNext()) { 
       try { 
        // Delete the SMS 
        String pid = c.getString(0); // Get id; 
        String uri = "content://sms/conversations/" + pid; 
        context.getContentResolver().delete(Uri.parse(uri), 
          null, null); 
       } catch (Exception e) { 
        Log.v("exception","occurred"); 
       } 
      } 

     } 

    } 
} 

}

什麼是錯的代碼?短信沒有被清除

回答

12

您可以使用從收件箱中刪除短信以下方法,

private void deleteMessage() 
{ 
    Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null); 
    //c.moveToFirst(); 

    while (c.moveToNext()) 
    { 
     System.out.println("Inside if loop"); 

     try 
     { 
      String address = c.getString(2); 
      String MobileNumber = mainmenu.getParameterData().getMobileNumber().trim(); 

      //Log.i(LOGTAG, MobileNumber + "," + address); 

      Log.i(LOGTAG, c.getString(2)); 


      if (address.trim().equals(MobileNumber)) 
      { 
       String pid = c.getString(1); 
       String uri = "content://sms/conversations/" + pid; 
       getContentResolver().delete(Uri.parse(uri), null, null); 
       stopSelf(); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

將這個代碼刪除所有收件箱消息 – Erum 2015-02-11 05:04:56

+0

無法在Android 4.3版上工作 – NarendraJi 2015-04-14 12:22:55

+0

這項工作將在5.0版本中完成嗎?使用相同的代碼將空遊標返回。 – Akshat 2015-04-28 14:11:03

10

嘗試一下本作刪除完整的解決方案...

public void deleteSMS(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()); 
    } 
} 
+0

是的,它爲我工作。 – Tomcat 2013-04-30 08:03:01

相關問題