Android Question forward sms (simple text received) to another cellphone

Art

Member
Licensed User
Longtime User
I'm trying to forward some incoming sms to another phone. My phone has Android 4.0.4. Latest B4A.
Here is what I have (mostly based on Erel tutorial 'Intent Filters - Intercepting SMS messages in the background'). When SMS is received, the process reaches all parts of the code but the forward does not go. No errors. Can you help?

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="14"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
'End of default text.
'----by me
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(s1,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)
'----end by me

Main
B4X:
Sub Process_Globals
End Sub

Sub Globals
    Private btStop As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    StartService(s1)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub AutoClose
   Log("+++ reached Main AutoClose. Stopping Service s1 and finishing Activity...")
   StopService(s1)
   Activity.Finish  
End Sub

Sub Forward(Number As String, Text As String)
Log("+++ Reached Main Sub Forward")
Dim sndsms As PhoneSms
sndsms.Send(Number, Text)
Log("+++ Was it sent? Closing anyway.")
AutoClose
End Sub

Sub Refreshdata
Log("+++ Reached the empty Main Sub Refreshdata")
End Sub

Sub btStop_Click
    Log("Stop Button clicked. Autoclosing...")
    AutoClose
End Sub

Service (s1)
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
End Sub

Sub Service_Create
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
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")
   
         If messages(i).Address = "+351XXXXXXXXX" Then
          Log("+++ Is from the expected number")
          Log("+++ Checking or Refreshing Main activity")
             If IsPaused(Main) = True Then
                  Log("+++ Main Activity is paused, Calling Refreshdata")
                  CallSub(Main, "RefreshData")
            End If
         Log("+++ Calling Main Forward")  
         CallSub3(Main, "Forward", "+35188888888", "teste")  
         Log("+++ Calling AutoClose")
         CallSub(Main, "AutoClose")   
         End If       
   
      Next
   End If
   Return messages
End Sub

Sub Service_Destroy

End Sub

Thanks.
 
Last edited:

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
Could it be the length of the message to be sent (take a look at multipart sms in the forum) or perhaps the Main activity is paused (I would have put the SndSMS.Send in a service).
 
Upvote 0

Art

Member
Licensed User
Longtime User
Hello,
Could it be the length of the message to be sent (take a look at multipart sms in the forum) or perhaps the Main activity is paused (I would have put the SndSMS.Send in a service).
The messages are not long or multipart, they are simple plain text with only a few words. Anyway the problem is not in the incoming sms. Incoming sms are recognized as expected (it's Erel code). The problem is the forward. It seems the send sms becomes blocked after receiving an incoming sms. I'll try to move the sendsms to a service. Thank you lemonisdead :).
 
Last edited:
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
I didn't thought about receiving but sending the SMS. But if you are sure the length is correct, that's not the problem.
Have you already tried to send the SMS from a service or alone ?
 
Upvote 0

Art

Member
Licensed User
Longtime User
Hello,
I didn't thought about receiving but sending the SMS. But if you are sure the length is correct, that's not the problem.
Have you already tried to send the SMS from a service or alone ?
What do you mean by "alone"? It's in the Main module. In a test version of the same app, I put a button that sends a message before starting the interception service and the sms is sent. After that the service is launched and when a messages comes in, it is recognized/intercepted, written to the Log, etc. But the forward of the body text or a simple send sms for testing purpose only doesn't work. As I told in my previous answer, I'll try to put the send sms code in another separate service but I don't know if that would be necessary or if there is something wrong with my phone which is a Samsung DUOS (two phone cards). Thanks.
===
Update: I put the forward sms Sub in another service module (s2) but it still doesn't work, the sms is not sent.
 
Last edited:
Upvote 0
Top