Android Question Trying to intercept SMS messages

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
Hi, Ive followed the Erel tutorial instructions in:
but it does not work.

Ive adder to the manifest:
B4X:
AddReceiverText(readsmsservice,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)

Created a Service called "readsmsservice" and complete it like the tutorial.
The service starts ok but it does nothing when sms arrives.

Im using b4a 9.80

Anything I missed?
Thanks
 

kisoft

Well-Known Member
Licensed User
Longtime User
Show the service code and write what you want to achieve when an SMS appears.
 
Upvote 0

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
@Erel, Im compiling in release mode, I see the service starting but because Im starting it with StartService from anothet activity.
In that activity I handle the permissions using:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   
    Activity.LoadLayout("EnvioSMS")
    Activity.Title = Chr(0xF1FA) & "Envio de SMS/MAIL"
   
    rp.CheckAndRequest(rp.PERMISSION_SEND_SMS)
    rp.CheckAndRequest(rp.PERMISSION_READ_SMS)
   
    Timer1.Initialize("Timer1",5000)
    Timer1.Enabled = False
       
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    If Permission = rp.PERMISSION_SEND_SMS Then
        'gmap.MyLocationEnabled = Result
    End If
End Sub

Log:
B4X:
Registo conectado a:  HUAWEI ALE-L23
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Params no existe y dbInstalada= 1
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (enviosms) Create, isFirst = true **
** Activity (enviosms) Resume **
** Activity (enviosms) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (enviosms) Resume **
>>>>>>>>>>>>>>>>>>>>>>here is when i push a button and start de service<<<<<<<<<<<<<<<<<
*** Service (readsmsservice) Create ***
** Service (readsmsservice) Start **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **

This is the service code:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
   
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type Message (Address As String, Body As String)
End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
    'Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    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
    Service.StopAutomaticForeground
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
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
This is a fragment of the code of my application that I had to withdraw from the store.
B4X:
Sub Process_Globals
    Type Message (Address As String, Body As String)
    Dim dane1 As String
    Dim dane2 As String
   
End Sub

Sub Service_Create
   
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
   
End Sub

Sub Service_Start(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
   
    Service.StopAutomaticForeground
   
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")
            dane1= messages(i).Body
            log(dane1)
            dane2= messages(i).Address
            akcja
            log(dane2)
            Service.StopAutomaticForeground
        Next
    End If
    Return messages
   
End Sub

Sub akcja

            Service.StopAutomaticForeground
            CallSubDelayed(Main, "startalarm")
            StartActivity("main")
            StopService(Me)  
End Sub
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
Set for tests in the manifest...You have a problem with permissions...
B4X:
android:targetSdkVersion="24"
 
Upvote 0

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
Ok, using wait for and not starting the service.
Im asked for Send but not asked for Read.
Logging the permission results...
** Activity (enviosms) Create, isFirst = true **
** Activity (enviosms) Resume **
Permiso send SMS: true
** Activity (enviosms) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
Permiso read SMS: false <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Why?
** Activity (enviosms) Resume **

My new code:
B4X:
    rp.CheckAndRequest(rp.PERMISSION_SEND_SMS)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        'Flow program continues if true
        'Call a sub or put your routine here
    End If
    Log("Permiso send SMS: " & Result)
    
    rp.CheckAndRequest(rp.PERMISSION_READ_SMS)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        'Flow program continues if true
        'Call a sub or put your routine here
    End If
    Log("Permiso read SMS: " & Result)
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
android.permission.READ_PHONE_STATE. Maybe you still need it. You don't have to guess, just check ...
 
Upvote 0

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
Ive watched the video tutorial, thanks, very goor info.
Both permissions are Dangerous like you can see in the attached pic
This is my manifest:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(readsmsservice,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)
And the code used is in my last post.
I find all is ok but Im not asked for the RECEIVE_SMS permission and then the permission is always FALSE.
Dont know what else to do.
 

Attachments

  • 1.png
    1.png
    20.8 KB · Views: 195
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
Add it manually to the manifest
B4X:
AddPermission(android.permission.SEND_SMS)
 
Upvote 0
Top