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?