I need an App that starts itself when a message either from a specific number or with a specific text comes in. I set up a SMS Broadcast Receiver, setup the permissions in the AndroidManifest, and in the Main Activity, I created an Intent. I tried a lot with ChatGPT and DeepSeek Coder, but i just can't figure it out.
SmsReceiver.kt:
package com.......
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Telephony
import android.telephony.SmsMessage
import android.util.Log
import android.os.Bundle
import android.widget.Toast
class SmsReceiver : BroadcastReceiver() {
val SMS ="android.privider.Telephony.SMS_RECEIVED";
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Telephony.Sms.Intents.SMS_RECEIVED_ACTION) {
Log.d("SmsReceiver", "SMS received intent detected")
val smsMessages = Telephony.Sms.Intents.getMessagesFromIntent(intent)
for (message in smsMessages) {
val sender = message.displayOriginatingAddress
val messageBody = message.messageBody
Log.d("SmsReceiver", "Sender: $sender")
Log.d("SmsReceiver", "Message body: $messageBody")
if (sender == "+43....." || messageBody.contains("Einsatzgebiet")) {
Log.d("SmsReceiver", "Message received from: $sender")
Log.d("SmsReceiver", "Message body: $messageBody")
val launchIntent = Intent(context, MainActivity::class.java)
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
launchIntent.putExtra("sms_body", messageBody)
context.startActivity(launchIntent)
}
}
}
}
}
permissions in my AndroidManifest:
<uses-feature
android:name="android.hardware.telephony"
android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
and:
<receiver
android:name=".SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
MainActivity.kt:
at the onCreate:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS), REQUEST_SMS_PERMISSION)
}
val smsBody = intent.getStringExtra("sms_body")
if (smsBody != null) {
Log.d(TAG, "SMS content: $smsBody")
clipboardTextView.text = smsBody
updateEinsatzInfo(smsBody)
}
loadClipboardButton.setOnClickListener {
Log.d(TAG, "Load Clipboard button clicked")
updateClipboardContent()
}
later
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_SMS_PERMISSION) {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Log.d(TAG, "SMS permissions granted")
} else {
Log.e(TAG, "SMS permissions denied")
}
} else if (requestCode == REQUEST_CALL_PERMISSION) {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
makePhoneCall()
} else {
Log.e(TAG, "Call permission denied")
}
}
}
I also have some other functions, that don't really matter here (at least I think so)
I did a lot of logging in LogCat, especially in the onReceive of the SMSReceiver, but when opening logcat, no logs from SmsReceiver are shown.
When running the app, I just connected my Pixel 7 Pro via USB with debugging enabled, ran the app from android studio on my phone and testet it. When first launching the app, i get the SMS permission request, i granted it.
But no matter what I do, i can't get it to run properly. The other parts of the app work great. When i send a message from another phone containing any text to my phone, my phone does nothing. It doesn't start the app, no logs are shown, no clipboard content is set, nothing.
I checked the settings of the app, permissions are granted. i reinstalled the app multiple times, tried with the app in the foreground, with the app in the background and with the app killed. Nothing worked.
I am a beginner in android programming, so please be patient. If there are any questions, just ask.
Thanks! Mario
Activityfrom the background like that anymore, though, unless you have a certain permission, so I wouldn't expect that part to work, but I'd think you should be seeing something from the Receiver in the logs.SYSTEM_ALERT_WINDOWpermission. Otherwise, you should post aNotificationinstead, and let the user launch theActivityfrom that.