title: Deleting Android SMS programmatically
body: <p>I want to delete some certain SMS automatically in my Android application. Therefore I have a method which does exactly what I want it to do. However, it only works if I deploy the application directly to my phone from Eclipse. Then it deletes incoming SMS. However, it does not work if the application is downloaded from the market. But there is also no error. Does anybody know how I can solve this or does this only work on rooted devices?</p>
  
  <pre><code>public void deleteSMS(Context context, String message, String number) {
      try {
          mLogger.logInfo("Deleting SMS from inbox");
          Uri uriSms = Uri.parse("content://sms/inbox");
          Cursor c = context.getContentResolver().query(uriSms,
              new String[] { "_id", "thread_id", "address",
                  "person", "date", "body" }, null, null, null);
  
          if (c != null &amp;&amp; c.moveToFirst()) {
              do {
                  long id = c.getLong(0);
                  long threadId = c.getLong(1);
                  String address = c.getString(2);
                  String body = c.getString(5);
  
                  if (message.equals(body) &amp;&amp; address.equals(number)) {
                      mLogger.logInfo("Deleting SMS with id: " + threadId);
                      context.getContentResolver().delete(
                          Uri.parse("content://sms/" + id), null, null);
                  }
              } while (c.moveToNext());
          }
      } catch (Exception e) {
          mLogger.logError("Could not delete SMS from inbox: " + e.getMessage());
      }
  }
  </code></pre>