首页 > 解决方案 > 接收短信 Kotlin

问题描述

我的 Kotlin 应用程序发送和接收消息,但是Toast.makeText()当我收到消息时它不起作用。我在下面包含了我的文件。到目前为止我尝试过的所有解决方案都不起作用。

问题:如何使Toast.makeText()to 工作。

清单.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>

短信接收器.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()工作的方式吗?

标签: kotlin

解决方案


在 Manifest.xml 中

无需在接收器中添加类别

 <receiver
            android:name=".SMSReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
  • 你可以试试这个功能

    private fun receivemsg() {
             var br  = object : BroadcastReceiver() {
                 override fun onReceive(p0: Context?, p1: Intent?) {
                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                        for (sms:SmsMessage in Telephony.Sms.Intents.getMessagesFromIntent(p1)){
                            Toast.makeText(applicationContext,sms.displayMessageBody,Toast.LENGTH_LONG).show()
                        }
                     }
                 }
             }
             registerReceiver(br, IntentFilter("android.provider.Telephony.SMS_RECEIVED"))
         }
    

推荐阅读