SMS Question

scarr

Member
Licensed User
Longtime User
Hi All,

OK I have being trying to avoid asking but I think I am now going round in circles, please tell me what I am doing wrong and why SI_MessageReceived do not appear to get called?


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: 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$")
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(S1,
<intent-filter>
   <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)

'End of default text.

Main

B4X:
#Region  Project Attributes 
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName: 
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: 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.
   Dim phone1 As Phone
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 Button1 As Button
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("Layout1")
   Activity.LoadLayout("main")
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub Button1_Click
   result = Msgbox2("Are you sure you want to exit","Alert","Yes","No","",Null)
End Sub

S1
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.
   Dim SI As SmsInterceptor 
End Sub

Sub Service_Create
   SI.Initialize2("SI",999) '2147483647
End Sub

Sub Service_Start (StartingIntent As Intent)
   If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
      StartService("")
       ToastMessageShow(" ---- ", True)
   End If
End Sub

Sub Service_Destroy
   StopService("")
End Sub

Sub SI_MessageReceived(From As String, Body As String) As Boolean
    ToastMessageShow(From & " !!! " & Body, True)
   Return True
End Sub

Thanks Steve
 

scarr

Member
Licensed User
Longtime User
Hmmmm

Hi,

I see quite a few people have looked at my post but no replies, I thought it would be obvious what the problem is, do I need to post more info?

Steve
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
I think you are mixing two approches

1. register the service for OS events which you are doing in the manifest file, in that case your service_start will be called for each sms_recieved - so look at the intent , extract the info and react, no need for the rest of the code
2.use the phone lib approch, register for MessageReceived using si.init...

my guess is that your si.init runs too late after the event has been fired alreday (because it run in the service_create which run after the event been fired (from the OS (see manifest))
if you want to use the phone lib approch you need to move that code to the application or start the service from the application, the main problem is that if the app is down the event will not wake the app/service if you only use the phone line approch

I suggest you use approch 1, just write your sms code in the service start sub using the intent as the data source
 
Upvote 0

DKCERT

Member
Licensed User
Longtime User
You are calling StartService from within the service-module itself. Move the call to the Main under Activity_Create and call it: StartService(S1). You should call StopService(S1) from Main as well.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…