Skip to content

Instantly share code, notes, and snippets.

@MohitSharma0101
Created September 14, 2021 09:13
Show Gist options
  • Save MohitSharma0101/b3c980f1cd74e24f84607fddd8b51145 to your computer and use it in GitHub Desktop.
Save MohitSharma0101/b3c980f1cd74e24f84607fddd8b51145 to your computer and use it in GitHub Desktop.
//Make sure to handle read SMS permission before calling this function
fun getSmsCountByNumber(senderNumber:String) : Int{
var totalSMS = 0
val countryCode = "+91"
applicationContext.contentResolver.query(
Telephony.Sms.CONTENT_URI,
null,
null,
null,
null)?.use { cursor ->
while (cursor.moveToNext()) {
val number = cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Sms.ADDRESS));
if (number == senderNumber || number == countryCode + senderNumber ) {
totalSMS += 1
}
}
Log.d("totalSMSCount",totalSMS.toString())
}
return totalSMS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment