I can't seem to use BroadcastReceiver() to get mms data without Cursor() so I tried to use a workaround with ComponentActivity() and registerReceiver(). Does anyone know how to do this with Kotlin? I really need to pass parts of the mms data by passing values to another function. I can't use this to register the class in the manifest. Is that the problem?
package radwil.sms.routing
import android.annotation.SuppressLint
import android.content.*
import android.database.Cursor
import android.net.Uri
import android.os.Bundle
import android.provider.Telephony.Sms.Intents.WAP_PUSH_RECEIVED_ACTION
import android.text.TextUtils
import androidx.activity.ComponentActivity
import radwil.sms.utils.log
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
/**
* This class used to monitor SMS
*/
class MmsReceiverK: ComponentActivity() {
// this is the second problem's solution for now
@SuppressLint("Range")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val receiver = object: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
log( "+++++inside receiver")
}
}
val filterMMS = IntentFilter.create("android.provider.Telephony.WAP_PUSH_RECEIVED", "application/vnd.wap.mms-message")
registerReceiver(receiver, filterMMS)
filterMMS.priority = IntentFilter.SYSTEM_HIGH_PRIORITY
filterMMS.addAction(WAP_PUSH_RECEIVED_ACTION) // MMS
if (TextUtils.equals(intent.action, WAP_PUSH_RECEIVED_ACTION)) {
//handle the WAP_PUSH_RECEIVED_ACTION
val contentResolver: ContentResolver = this.contentResolver
val projection = arrayOf("_id", "ct_t", "Address")
val uri: Uri = Uri.parse("content://mms-sms/conversations/")
val query: Cursor? = contentResolver.query(uri, projection, null, null, null)
if (query != null) {
if (query.moveToFirst()) {
do {
// my second problem is an empty query.getColumnIndex?
val string: String = query.getString(query.getColumnIndex("ct_t"))
if ("application/vnd.wap.multipart.related" == string) {
// mms
val mmsId = query.getColumnIndex("_id")
val mmsAddress = query.getColumnIndex("Address")
val body = getMmsText(mmsId)
log(
"received SMS from: $mmsAddress" +
"with message: $body"
)
} else {
// sms
// return?
}
} while (query.moveToNext())
}
query.close()
}
}
}
// parsing "part" for text of message
private fun getMmsText(id: Int): String {
val partURI = Uri.parse("content://mms/part/$id")
var `is`: InputStream? = null
val sb = StringBuilder()
try {
`is` = this.contentResolver.openInputStream(partURI)
if (`is` != null) {
val isr = InputStreamReader(`is`, "UTF-8")
val reader = BufferedReader(isr)
var temp: String = reader.readLine()
while (true) {
sb.append(temp)
temp = reader.readLine()
}
}
} catch (e: IOException) {
} finally {
if (`is` != null) {
try {
`is`.close()
} catch (e: IOException) {
}
}
}
return sb.toString()
}
}