Android Question [Solved]Read OTP [SMS] and Insert into text Field

AndroidMadhu

Active Member
Licensed User
I have written the below code at Starter Service:
B4X:
#Region   Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#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
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.

End Sub

Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
Log("Start service")
If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
Dim messages() As Message
Dim Messaggio() As String
Dim Stato As String
messages = ParseSmsIntent(StartingIntent)
For i = 0 To messages.Length - 1
Log(messages(i))
                      
Messaggio=Regex.Split ("?",messages(i).Body)
                      
                      
Next
End If
Service.StopAutomaticForeground
End Sub

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

Sub Service_TaskRemoved
'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub

Sub Service_Destroy

End Sub

But no SMS has been received so far....

Please advice...

Thanks
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
HI
I don't know if the starter module is a good place. It should be in the taj service as in the example ... I am sending code snippets from my application ...
I don't know if the startup module is a good place. It should be in the service as in the example ... I'm sending code snippets from my application ...


B4X:
#Region  Service Attributes
    #StartAtBoot: False
   
#End Region

Sub Process_Globals
    Type Message (Address As String, Body 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 as string = messages(i).Body
            dane2 as string = messages(i).Address
            'Here you can use callsubdylayed and transfer data to Activity
           
            'StartActivity(Main)
           
            Service.StopAutomaticForeground
        Next
    End If
    Return messages
   
End Sub
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Just create a new service for this. DO NOT use the starter service for this.
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
@DonManfred .. I have created a new service named s1 and update the cade base. But when I am sending SMS from other mobile, the SMS is not read by the program and I am not able to found any log at the log window with USB connected device. The Service_start is not calling at all.

B4X:
#Region   Service Attributes
#StartAtBoot: False

#End Region

Sub Process_Globals
Type Message (Address As String, Body As String)
End Sub
Sub Service_Create

End Sub

Sub Service_Start(startingIntent As Intent)
Log("Start service")
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

Manifest Details :
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
      android:normalScreens="true"
      android:smallScreens="true"
      android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(s1,
<intent-filter>
      <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)

Please advice
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
B4X:
messages(i).Body = r.RunMethod("getMessageBody")
messages(i).Address = r.RunMethod("getOriginatingAddress")

This is a message and a phone number ... You must now open the activity and pass these parameters to this activity. Only in an open activity, you can perform a phone response, for example turn on ringtones or display a message, whatever you want.
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
First, do this and see if the main activity opens (When any SMS arrives)...

B4X:
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")
            StartActivity(Main)
           
            Service.StopAutomaticForeground
        Next
    End If
    Return messages
   
End Sub
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
allow sms receive permission in runtime
B4X:
rp.CheckAndRequest(rp.PERMISSION_RECEIVE_SMS)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
        MsgboxAsync("No permission to read sms", "")
        Wait For Msgbox_Result(Result As Int)
        
End If
 
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
You test your application on DEBUG or RELEASE mode? Try on Release mode
You try to clean your project?
 
Upvote 0
Top