1

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

ndc85430
1,7743 gold badges15 silver badges26 bronze badges
asked Jul 31, 2024 at 17:10
5
  • Your SMS setup looks OK, and indeed does work as expected for me: i.sstatic.net/DaPyxgn4.png. You can't really launch an Activity from 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. Commented Jul 31, 2024 at 18:02
  • @MikeM. thanks for testing. How did you send the SMS? With a real phone or with ADB? What else could I use to open an Activity after receiving an SMS? right now i am using macrodroid to copy the SMS content to clipboard and then open my app. That works good, but i want to install ONLY this app without Macrodroid on other phones as well. Commented Jul 31, 2024 at 18:12
  • It's on a Samsung running 14. Sent the message to myself. Commented Jul 31, 2024 at 18:13
  • You generally don't want to launch anything from the background like that, just 'cause users don't like it, but if you're sure your users are OK with it, you can do it with the SYSTEM_ALERT_WINDOW permission. Otherwise, you should post a Notification instead, and let the user launch the Activity from that. Commented Jul 31, 2024 at 18:17
  • 1
    @MikeM. Just implemented that. It works aweasome! Thank you! And yes this is necessary. I am a first responder paramedic here in austria. When a emergency occurs in my neighborhood, i get a message with the adress, the code of the emergency and some other details. Every second counts so i wanted to build an app to get the message details and quickly open google maps with the adress and to call 144 (our emergency number to tell them, that i am coming to the emergency). I would like other paramedics to have this app as well. Thanks again for your help! Commented Jul 31, 2024 at 19:21

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.