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.
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:
I have my manifest set as per below:
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:
This returns True.
Anyone know where I have gone wrong ?
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:
Intent Filters - Intercepting SMS messages in the background
Old tutorial. Don't use services. Use receivers. Broadcast receivers are program components that can handle broadcasted messages. These messages usually notify about a system event. There are two types of receivers in Android: statically registered receivers and dynamically registered...
www.b4x.com
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 ?