Android Question broadcastreceiver for receiving SMS

RAZRO

Member
hello everyone
I am trying to make an app in which I need to see SMS information
at first I used to use service from this tutorial(https://www.b4x.com/android/forum/t...messages-in-the-background.20103/#post-116193)
that was great and I could see the message and the sender number(they are important to me)
But the problem is this service is not working in background in android 13 so I tried to use receiver and I check this tutorial(https://www.b4x.com/android/forum/threads/broadcastreceiver.12493/)
this code is not working for me now and I dont know how to get SMS information using broadcastreceiver.
thanks in advance
 

DonManfred

Expert
Licensed User
Longtime User
Is there anyway I can see All messages for special number from SMS manager?
only if your app is the selected messaging app
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
there is a difference between being the default messaging app and an app that can receive
a broadcast when an sms is received. technically you can do both. the first is difficult, the
second is easy. do you understand the difference?

if all you want to do is to see when an sms is received (phone number and text),
you can do this. i do it on devices running android 12 and 14. when my devices receive an
sms, both the default sms app and my app receive the broadcast. the default sms app
does whatever it's supposed to do, and my app does what it's supposed to do. no problem,
no conflict. they are functioning independently. my app runs all the time and starts by itself
when the device is turned on.

you need to edit the manifest (add permissions and a receiver), and you need a receiver.
i don't know what you expect to do in the receiver, but it can't be something that takes too long.
you may need the receiver to communicate with a service to handle some long-running task in
that case. you need to define what it is you're trying to do and to familiarize yourself with how
b4a works with receivers and services these days. and you really need to know what android
requires for a given sdk. some of these issues are automatically handled in each successive
version of b4a, but you need to understand what they are and why certain things don't work.

you should research recent posts relating to receivers, not posts and examples from 10 years
ago. many things have changed with android since then.

start here: https://www.b4x.com/android/forum/threads/receivers-and-services-in-2023.145370/#content
get that example running. it will be easy to modify it to handle sms.
 
Upvote 0

RAZRO

Member
there is a difference between being the default messaging app and an app that can receive
a broadcast when an sms is received. technically you can do both. the first is difficult, the
second is easy. do you understand the difference?

if all you want to do is to see when an sms is received (phone number and text),
you can do this. i do it on devices running android 12 and 14. when my devices receive an
sms, both the default sms app and my app receive the broadcast. the default sms app
does whatever it's supposed to do, and my app does what it's supposed to do. no problem,
no conflict. they are functioning independently. my app runs all the time and starts by itself
when the device is turned on.

you need to edit the manifest (add permissions and a receiver), and you need a receiver.
i don't know what you expect to do in the receiver, but it can't be something that takes too long.
you may need the receiver to communicate with a service to handle some long-running task in
that case. you need to define what it is you're trying to do and to familiarize yourself with how
b4a works with receivers and services these days. and you really need to know what android
requires for a given sdk. some of these issues are automatically handled in each successive
version of b4a, but you need to understand what they are and why certain things don't work.

you should research recent posts relating to receivers, not posts and examples from 10 years
ago. many things have changed with android since then.

start here: https://www.b4x.com/android/forum/threads/receivers-and-services-in-2023.145370/#content
get that example running. it will be easy to modify it to handle sms.
Thanks for your answering
I think I Know the difference
and I want to create an app listening to sms receives and if the number is equal to one special number, save the text in a file
but I need this program work in background and save the message even the program is not running
I will study the threads you sent
Can you send the code in wich a receiver get the sms and send it to a service? If you have it
 
Upvote 0

RAZRO

Member
there is a difference between being the default messaging app and an app that can receive
a broadcast when an sms is received. technically you can do both. the first is difficult, the
second is easy. do you understand the difference?

if all you want to do is to see when an sms is received (phone number and text),
you can do this. i do it on devices running android 12 and 14. when my devices receive an
sms, both the default sms app and my app receive the broadcast. the default sms app
does whatever it's supposed to do, and my app does what it's supposed to do. no problem,
no conflict. they are functioning independently. my app runs all the time and starts by itself
when the device is turned on.

you need to edit the manifest (add permissions and a receiver), and you need a receiver.
i don't know what you expect to do in the receiver, but it can't be something that takes too long.
you may need the receiver to communicate with a service to handle some long-running task in
that case. you need to define what it is you're trying to do and to familiarize yourself with how
b4a works with receivers and services these days. and you really need to know what android
requires for a given sdk. some of these issues are automatically handled in each successive
version of b4a, but you need to understand what they are and why certain things don't work.

you should research recent posts relating to receivers, not posts and examples from 10 years
ago. many things have changed with android since then.

start here: https://www.b4x.com/android/forum/threads/receivers-and-services-in-2023.145370/#content
get that example running. it will be easy to modify it to handle sms.
hello drgottjr
I have searched for receiver as you said and I make this app for a receiver :
B4X:
Sub Process_Globals
    Type Message (Address As String, Body As String)
End Sub

'Called when an intent is received.
'Do not assume that anything else, including the starter service, has run before this method.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
        Dim messages() As Message
        messages = ParseSmsIntent(StartingIntent)
        For i = 0 To messages.Length - 1
            Log(messages(i))
            ToastMessageShow(messages(i),True)
        Next
    End If

End Sub

'Parses an SMS intent and returns an array of messages
Sub ParseSmsIntent (in As Intent) As Message()
    Dim messages() As Message
    If in.HasExtra("pdus") = False Then Return messages
    Dim pdus() As Object
    Dim r As Reflector
    pdus = in.GetExtra("pdus")
    If pdus.Length > 0 Then
        Dim messages(pdus.Length) As Message
        For i = 0 To pdus.Length - 1
            r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
            Array As Object(pdus(i)), Array As String("[B"))
            messages(i).Body = r.RunMethod("getMessageBody")
            messages(i).Address = r.RunMethod("getOriginatingAddress")
        Next
    End If
    Return messages
End Sub

and this is my manifest:
B4X:
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(smsmessage,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)

now this code works fine for android less than 12 in background
But in xiaomi with android 14 this code just works when the app is open. It does not working in the background
Can you help me to understand what is the problem?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
what you have done is basically what i have done. it works for me (even when app is in background), so it should work for you. i'm running android 12 and 14.
there could be an issue with xiaomi's version of android.
add these to your manifest: (you already have RECEIVE_SMS; these are different)

B4X:
AddPermission(android.provider.Telephony.SMS_RECEIVED)
AddPermission(android.permission.READ_SMS)
AddPermission(android.permission.READ_PHONE_STATE)
AddPermission(android.permission.POST_NOTIFICATIONS)

you probably don't need READ_PHONE_STATE. i use it for something else.

you don't need a service. i log all messages to a file from the receiver. if you must practice with a server, use @Erel's famous background tracking example. you start a foreground service, you add the receiver (as you have already done), and the receiver uses callsubdelayed() to run a sub in the service. but for simply logging messages from a certain number, you can do that right in the receiver by itself. that's what i do.
 
Last edited:
Upvote 0
Top