如何解决短信接收器Android Studio Kotlin
我想做一个SMS接收器。我的应用程序应该会收到一条短信,并显示一条消息“已检测到短信”。
我上了MyReciever.kt类
package com.example.messageresender
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.telephony.SmsMessage
import android.widget.Toast
class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context,intent: Intent) {
Toast.makeText(context,"sms detected",Toast.LENGTH_SHORT).show()
}
}
并添加到android清单
<receiver
android:name=".MyReceiver"
<intent-filter android:priority="9999">
<action android:name="android.provider.telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
androidManifest.xml现在看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.messageresender">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_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"
android:fullBackupContent="@xml/backup_descriptor">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
<intent-filter android:priority="9999">
<action android:name="android.provider.telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
,它根本不起作用。当我将SMS发送到仿真的android设备时,未显示“检测到SMS”消息。我在android 7.1(不是oreo)上进行了尝试,因为互联网上有人说BroadCastReciever无法在android oreo上运行,因为我应该注册它,但是我做不到,因为当我这样做时,我的应用程序崩溃了。但这仍然行不通
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
var syReceiver = MyReceiver()
val intentFilter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
intentFilter.addAction(Telephony.Sms.Intents.DATA_SMS_RECEIVED_ACTION)
this.registerReceiver(syReceiver,intentFilter)
}
请帮助我。我想发短信!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。