Попробуйте использовать это для Java
public class SmsBroadcastReceiver extends BroadcastReceiver {
public static final String SMS_BUNDLE = "pdus";
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsBody = smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
//this will update the UI with message
SmsActivity inst = SmsActivity.instance();
inst.updateList(smsMessageStr);
}
}
}
Попробуйте использовать что-то подобное для Kotlin
class SmsBroadcastReceiver:BroadcastReceiver() {
fun onReceive(context:Context, intent:Intent) {
val intentExtras = intent.getExtras()
if (intentExtras != null)
{
val sms = intentExtras.get(SMS_BUNDLE) as Array<Any>
val smsMessageStr = ""
for (i in sms.indices)
{
val smsMessage = SmsMessage.createFromPdu(sms[i] as ByteArray)
val smsBody = smsMessage.getMessageBody().toString()
val address = smsMessage.getOriginatingAddress()
smsMessageStr += "SMS From: " + address + "\n"
smsMessageStr += smsBody + "\n"
}
Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show()
//this will update the UI with message
val inst = SmsActivity.instance()
inst.updateList(smsMessageStr)
}
}
companion object {
val SMS_BUNDLE = "pdus"
}
}