Delete SMS

DanielGr 1 Reputation point
2020-12-02T09:27:17.897+00:00

I am unable to delete an SMS.
Allways I try, I get no error but nothing is deleted (count = 0)
I have permission to ReadSms and WriteSms.
This is my code, called in MainActivity:

    public int DeleteSMS(string number)
    {
        int count = 0;
        ICursor cursor = ContentResolver.Query(Uri.Parse("content://sms/"), new string[] { "_id", "thread_id", "address" }, null, null, null);

        if (cursor != null && cursor.MoveToFirst())
        {
            do
            {
                string id = cursor.GetString(0);
                long threadId = cursor.GetLong(1);
                string address = cursor.GetString(2);

                if (address == number)
                {
                    count += ContentResolver.Delete(Uri.Parse("content://sms/" + id), null, null);
                }

            } while (cursor.MoveToNext());
        }
        return count;
    }

Any idea?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,951 Reputation points
    2020-12-02T11:13:14.63+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    On Android, the system only allows the default app to write message data to the provider, although other apps can read at any time. To delete an SMS in your application, you need to set the application as the default SMS app on the device.

    Tutorial:
    https://developer.android.com/about/versions/kitkat.html#44-sms-provider

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.