2017-02-09 101 views
-1

我使用此代碼(下面給出)刪除發送的短信,此代碼需要發件人的號碼(我的設備號碼),但我想刪除使用接收者的號碼(號碼在這我發送短信)。請告訴我該怎麼做?刪除發送短信使用接收者的號碼,而不是發件人的號碼

代碼:

public void deleteSMS(Context ctx, String message, String number) { 
     try { 
      Uri uriSms = Uri.parse("content://sms"); 
      Cursor c = ctx.getContentResolver().query(uriSms, new String[] { "_id", "thread_id", "address", "person", "date", "body" }, null, 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 (body.contains(getResources().getText(R.string.invite_text).toString()) && address.equals(number)) { 
        if (message.equals(body) && address.equals(number)) { 
         // mLogger.logInfo("Deleting SMS with id: " + threadId); 
         int rows = ctx.getContentResolver().delete(Uri.parse("content://sms/" + id), "date=?",new String[] { c.getString(4) }); 
         Log.e("log>>>", "Delete success......... rows: "+rows); 
         Log.e("log>>>", "Delete success......... body: "+body); 
        } 
       } while (c.moveToNext()); 
      } 

     } catch (Exception e) { 
      //Log.e("log>>>", e.toString()); 
      //Log.e("log>>>", e.getMessage()); 
      throw e; 
     } 
    } 

回答

0
public void deleteSMS(Context ctx, String message, String number) { 
    try { 
     Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox"); 
     Cursor cursor1 = ctx.getContentResolver().query(mSmsinboxQueryUri, 
       new String[]{"_id", "thread_id", "address", "person", "date", 
         "body", "type"}, null, null, null); 
     ((Activity)ctx).startManagingCursor(cursor1); 
     String[] columns = new String[]{"address", "person", "date", "body", "type"}; 
     if (cursor1.getCount() > 0) { 
      String count = Integer.toString(cursor1.getCount()); 
      Log.e("Count", count); 
      while (cursor1.moveToNext()) { 
       String type = cursor1.getString(cursor1.getColumnIndex(columns[4])); 
       if (type.equals("2")) // 2 for Sent Sms 
       { 
        String address = cursor1.getString(cursor1.getColumnIndex(columns[0])); 
        String name = cursor1.getString(cursor1.getColumnIndex(columns[1])); 
        String date = cursor1.getString(cursor1.getColumnIndex(columns[2])); 
        String body = cursor1.getString(cursor1.getColumnIndex(columns[3])); 
        if (message.equals(body) && address.equals(number)) { 
         // mLogger.logInfo("Deleting SMS with id: " + threadId); 
         int rows = ctx.getContentResolver().delete(Uri.parse("content://sms/" + id), "date=?", new String[]{c.getString(4)}); 
         Log.e("log>>>", "Delete success......... rows: " + rows); 
         Log.e("log>>>", "Delete success......... body: " + body); 
        } 
       } 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
相關問題