简体   繁体   中英

how to delete all sms of a specific contact number in android

I have the following code which delete all the sms conversation ,

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

but i want to delete sms of a specific contact number, What to do?

You can delete all SMS of a particular number by using,

private void removeMessage(Context context, String fromAddress) {

    Uri uriSMS = Uri.parse("content://sms/inbox");
    Cursor cursor = context.getContentResolver().query(uriSMS, null, 
                                                               null, null, null);
    cursor.moveToFirst();
      if(cursor.getCount() > 0){
        int ThreadId = cursor.getInt(1);
        Log.d("Thread Id", ThreadId+" id - "+cursor.getInt(0));
        Log.d("contact number", cursor.getString(2));
        Log.d("column name", cursor.getColumnName(2));

        context.getContentResolver().delete(Uri.
                parse("content://sms/conversations/"+ThreadId), "address=?", 
                                                      new String[]{fromAddress});
        Log.d("Message Thread Deleted", fromAddress);
      }
      cursor.close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM