Receiving SMS

BorisK

Member
Licensed User
Longtime User
Hello,

I am very new to B4A and have problem in receiving SMS. I am sorry for my very basic question but I can't find the answer overlooking the forum.

I can send SMS without problem. When receiving if I set a breakpoint into service modul the program stops and LOG shows the content of the message. But I can't access to it. I would like to transfer it into main module (into variables SMSMessage, SMSNumber).


Thank you very much.


Here is my code.

Main module:
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim txtNumber As EditText
   Dim txtText As EditText
   Dim SMSMessage As String     'I want to transfer SMS message here
   Dim SMSNumber As String

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("smstest")
   Msgbox("Welcome to SMS test", "")
End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub btnSend_Click
   SendMessage (txtNumber.Text , txtText.Text )
End Sub



Sub SendMessage(Phone As String, LongText As String)
 Dim Ph As PhoneSms
 Ph.Send(Phone, LongText)
End Sub


Service module:
B4X:
Sub Service_Destroy

End Sub


'Service module
Sub Process_Globals
  Type Message (Address As String, Body As String)
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")
  Next
  End If
  Return messages
End Sub


Manifest editor:
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: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" 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$")
'End of default text.

AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(sms,
<intent-filter>
  <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)
 
Top