kotlin - 接收短信 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()
工作的方式吗?
解决方案
在 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")) }
推荐阅读
- powershell - 在powershell中使用itextsharp按页拆分PDF
- c++ - 在 Android Studio 中添加 C/C++ 的包含路径
- swift - 惰性集合导致“不稳定”过滤器和排序崩溃
- python - Python Curses Backspace 检测在主窗口中有效,但在子窗口中无效
- sql - 使用同一 ID 下的多个相关行插入新行的 SQL 脚本
- python - Python3 - ModuleNotFoundError:没有名为“numpy”的模块
- ruby-on-rails - 创建 Stripe 客户时没有附加付款来源错误
- spring-cloud-config - Spring Config Cloud 和可变属性
- python - Python中的OOP纸牌
- python-3.x - Python/Panda:如何生成二维记录