-1

I want to delete sms from android inbox when I receive it in my app. I already researched stackoverflow and found the codes but it doesn't work. I am not sure this is because I am using the latest android, gradle and emulator.

    private fun deleteSMS(message: String, number: String): Boolean {
        try {
            Log.d(TAG, "Deleting SMS from inbox")

            val uri = Uri.parse("content://sms/inbox")
            val c = this.contentResolver.query(uri, null, null, null, null)
            c?.let {
                if(!it.moveToFirst()) {
                    it.close()
                    return false
                }

                do {
                    val id = it.getInt(0)
                    val threadId = it.getInt(1)
                    val address = it.getString(2)
                    val body = it.getString(5)

                    Log.d(TAG, "address, body: $address : $body")
                    this.contentResolver.delete(Uri.parse("content://sms/$id"), null, null)
                }while (it.moveToNext())
            }

            c?.close()
            return true
        }catch (e: Exception) {
            Log.e(TAG, e.message)
            return false
        }
    }

I tried this code in java but it also doesn't work. Anyone can help me?

  • You can only write/delete to `content://sms/` if your app is selected as the default sms app – Pawel Mar 16 '20 at 11:05
  • Can I make my app as the default app? If then, could you let me know how to do that? – Full Stack Mar 17 '20 at 12:06
  • you don't want to do that unless your app is supposed to handle all incoming/outgoing SMSes and MMSes - that is it has to be fully fledged messaging app. If it is you can follow tips from here https://stackoverflow.com/questions/21720657/how-to-set-my-sms-app-default-in-android-kitkat – Pawel Mar 17 '20 at 13:02

1 Answers1

0

Unfortunately, deleting SMS messages like that is no longer possible since Android 4.4 (KitKat), unless your app is the default SMS app or the device is rooted.

Also, the system now allows only the default app to write message data to the provider, although other apps can read at any time.

http://developer.android.com/about/versions/kitkat.html

If you try anyway, it silently fails - it won't delete anything, won't raise any exceptions and won't display error messages.

If you only intend to target 4.4 in your app, then this workaround might help, but it won't work in later versions.