Получение SMS-сообщения Kotlin - PullRequest
0 голосов
/ 20 апреля 2020
Приложение

My Kotlin отправляет и получает сообщения, но Toast.makeText() не работает при получении сообщения. Я включил мои файлы ниже. Все решения, которые я пробовал до сих пор, не работают.

ВОПРОС: Как заставить Toast.makeText() работать.

Манифест. xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <receiver
        android:name=".SmsReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter >
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </receiver>

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

SmsReceiver.kt

package com.example.sendmessege

import android.annotation.TargetApi
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.telephony.SmsMessage
import android.util.Log
import android.widget.Toast


class SmsReceiver : BroadcastReceiver() {

private val TAG: String = SmsReceiver::class.java.getSimpleName()
val pdu_type = "pdus"


@TargetApi(Build.VERSION_CODES.M)
override fun onReceive(
    context: Context?,
    intent: Intent
) { // Get the SMS message.
    val bundle = intent.extras
    val msgs: Array<SmsMessage?>
    var strMessage = ""
    val format = bundle!!.getString("format")
    // Retrieve the SMS message received.
    val pdus = bundle[pdu_type] as Array<Any>?
    if (pdus != null) { // Check the Android version.
        val isVersionM = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
        // Fill the msgs array.
        msgs = arrayOfNulls(pdus.size)
        for (i in msgs.indices) { // Check Android version and use appropriate createFromPdu.
            if (isVersionM) { // If Android version M or newer:
                msgs[i] = SmsMessage.createFromPdu(
                    pdus[i] as ByteArray,
                    format
                )
            } else { // If Android version L or older:
                msgs[i] =
                    SmsMessage.createFromPdu(pdus[i] as ByteArray)
            }
            // Build the message to show.
            strMessage += "SMS from " + msgs[i]?.originatingAddress
            strMessage += " :" + (msgs[i]?.messageBody) + "\n"
            // Log and display the SMS message.
            Log.d(TAG, "onReceive: $strMessage")
            Toast.makeText(context, strMessage, Toast.LENGTH_LONG).show()
        }
    }
 }


}

Я пропал что-то? Или это другой способ написать функцию onReceive(), чтобы заставить Toat.makeText() работать?.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...