Delete SMS messages on Android 4.4 and 7.0

263 Views Asked by At

I use this code to delete unread SMS messages, but it only works on Android 4.4, not on Android 7.0. What do I have to change in the code to make it work on Android 7.0?

void __fastcall TForm1::Button4Click(TObject *Sender)
{
  _di_JCursor cursor;
  _di_Jnet_Uri uri;

  uri = StrToJURI( "content://sms" );

  cursor = SharedActivity()->getContentResolver()->query( uri, nullptr, StringToJString(L"read = 0"), nullptr, nullptr );

  if ( cursor )
  {
      while( cursor->moveToNext() )
      {
          int adresidx = cursor->getColumnIndex( StringToJString(L"address") );
          int sms_id = cursor->getColumnIndex( StringToJString(L"_id") );
          String adres = JStringToString( cursor->getString(adresidx) );
          String smsid = JStringToString( cursor->getString(sms_id) );

            TJavaObjectArray__1<_di_JString> *arg = new TJavaObjectArray__1<_di_JString>(1);
            arg->Items[0] = StringToJString(smsid);
            SharedActivity()->getContentResolver()->Delete( uri, StringToJString("_ID=?"), arg );
      }
  }
}
1

There are 1 best solutions below

2
Bhuvanesh BS On

From 4.4 onwards, your app is not allowed to delete any SMS messages from the inbox unless it is the "default" SMS app.

From Android's own documentation:

Android 4.4 APIs

Beginning with Android 4.4, the system settings allow users to select a "default SMS app." Once selected, only the default SMS app is able to write to the SMS Provider and only the default SMS app receives the SMS_DELIVER_ACTION broadcast when the user receives an SMS or the WAP_PUSH_DELIVER_ACTION broadcast when the user receives an MMS. The default SMS app is responsible for writing details to the SMS Provider when it receives or sends a new message.

Other apps that are not selected as the default SMS app can only read the SMS Provider, but may also be notified when a new SMS arrives by listening for the SMS_RECEIVED_ACTION broadcast, which is a non-abortable broadcast that may be delivered to multiple apps. This broadcast is intended for apps that---while not selected as the default SMS app---need to read special incoming messages such as to perform phone number verification.

For more information, read the blog post, Getting Your SMS Apps Ready for KitKat

And from that blog:

if your app is designed to behave as the default SMS app, then while your app is not selected as the default, it's important that you understand the limitations placed upon your app and disable features as appropriate. Although the system writes sent SMS messages to the SMS Provider while your app is not the default SMS app, it does not write sent MMS messages and your app is not able to write to the SMS Provider for other operations, such as to mark messages as draft, mark them as read, delete them, etc.