store received SMS in the SMS provider
This commit is contained in:
parent
3eb1127c74
commit
654359eaea
|
@ -1,5 +1,6 @@
|
|||
package com.simplemobiletools.smsmessenger.extensions
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.database.Cursor
|
||||
import android.provider.ContactsContract
|
||||
|
@ -158,3 +159,15 @@ fun Context.getNameFromPhoneNumber(number: String): Int? {
|
|||
|
||||
return null
|
||||
}
|
||||
|
||||
fun Context.insertNewSMS(address: String, subject: String, body: String, date: Long) {
|
||||
val uri = Telephony.Sms.CONTENT_URI
|
||||
val contentValues = ContentValues().apply {
|
||||
put(Telephony.Sms.ADDRESS, address)
|
||||
put(Telephony.Sms.SUBJECT, subject)
|
||||
put(Telephony.Sms.BODY, body)
|
||||
put(Telephony.Sms.DATE, date)
|
||||
}
|
||||
|
||||
contentResolver.insert(uri, contentValues)
|
||||
}
|
||||
|
|
|
@ -3,9 +3,18 @@ package com.simplemobiletools.smsmessenger.receivers
|
|||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.provider.Telephony
|
||||
import com.simplemobiletools.smsmessenger.extensions.insertNewSMS
|
||||
|
||||
class SmsReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
|
||||
val messages = Telephony.Sms.Intents.getMessagesFromIntent(intent)
|
||||
messages.forEach {
|
||||
val address = it.originatingAddress ?: ""
|
||||
val subject = it.pseudoSubject
|
||||
val body = it.messageBody
|
||||
val date = it.timestampMillis
|
||||
context.insertNewSMS(address, subject, body, date)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue