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
Main
Service (s1)
Thanks.
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: