Android Question Read SMS Message

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am trying to Intercept / Read an SMS so I can view the message in my app. (This app won't be going on Google Play).

I can send the SMS from my app, and that part works fine.

I have been using the SmsInterceptor method in the past and it has worked fine, as per below.

B4X:
Sub Service_Create
    Dim SI As SmsInterceptor
    SI.Initialize2("SI", 999)
End Sub

Public Sub SI_MessageReceived (From As String, Body As String) As  Boolean    'ignore
        LogColor("Reply: " & Body, Colors.Blue)
End sub

Since Android 14, the above is no longer working.

I then looked at:


I have added a receiver to my app called smsmessage

I then added the following code to the smsmessage receiver module:

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))
        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

I have my manifest set as per below:

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="34"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)

SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")

SetApplicationAttribute(android:icon, "@mipmap/ic_launcher")
CreateResource(mipmap-anydpi-v26, ic_launcher.xml,
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@mipmap/background"/>
    <foreground android:drawable="@mipmap/foreground"/>
</adaptive-icon>
)

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

However, my app can't read the message. Nothing is shows up in the IDE Log.

I then checked to make sure I can receive SMS messages:

B4X:
Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_RECEIVE_SMS)
    Wait for Activity_PermissionResult(Permission As String, result As Boolean)
    Log(result) ' returns True

This returns True.

Anyone know where I have gone wrong ?
 

drgottjr

Expert
Licensed User
Longtime User
your mods should work. whatever else might be wrong is hard to say. maybe there's an issue with smsinterceptor (getting long in the tooth). there are a couple recent sms handlers using receivers available. they mostly look very much like what have done, but without smsinceptor. i have my own little "interceptor" on android 14 (and changing sdk to 34, like yours). works fine, even when device is dozing or whatever android calls it. you seem to have followed the new rules.
 
Last edited:
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
I have managed to get it working.

For some reason it magically started working.
 
Upvote 0
Top