SMS Intercept - uncaught error

MartinP

Member
Licensed User
Longtime User
Hi

I'm a new user of the tool so am still very much feeling my way around - apologies in advance if the following is down to my stupidity, but here goes anyway!

I'm trying to put together a simple SMS intercepter app - for no other reason than to get my head around services and the main activities etc.

Having looked at some of the code spread around the forums for this, I've come up with the following:

Activity code:
B4X:
'Activity module
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.

End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub

Sub Activity_Resume
   SMS_Start
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub SMS_Start
If Sms.ServiceRunning = False Then
   StartService(Sms)
   Sms.ServiceRunning = True
   ToastMessageShow("Running in background", True)
End If

End Sub

The service Module (called "SMS") is as follows:
B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

   Dim ServiceRunning As Boolean
   Dim SI As SmsInterceptor
    Dim Notification1 As Notification

End Sub
Sub Service_Create
    SI.Initialize2("SI", 999)
    Notification1.Initialize
    Notification1.Icon = "icon" 'use the application icon file for the notification
    Notification1.Vibrate = False
    Log("Service initialized")
End Sub

Sub Service_Start (StartingIntent As Intent)
    Notification1.SetInfo("SMS Example", "test started", Main)
    Notification1.Sound = False
    Service.StartForeground(1, Notification1) 
   Log("Service Started")
End Sub

Sub Service_Destroy
   SI.StopListening()
   Service.StopForeground(1)
End Sub
Sub SI_MessageReceived (From As String, Body As String)
   Log("Message Received From: " & From)
    ToastMessageShow(From, True)
   Log("Toast shown")
        Return True ' I've also tried Return False, as I don't want the standard SMS app to be skipped if possible
End Sub

All of the Log commands are triggered (in the filtered log tab), but I keep getting an app (or service) crash on the phone.

I've copied the (unfiltered) log snippet here in case it helps point out the error of my ways - I can upload the entire project/log file if needed.

B4X:
Message Received From: #MobileNumberHere#
Toast shown
SMS SC address: #ServiceCentreNumHere#
originatingAddress.address : #MobileNumberHere#
SMS SC timestamp: 1328142047000
hasUserDataHeader : false
messageBody : Test
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x4001e578)
GC_EXPLICIT freed 131K, 49% free 3081K/5959K, external 0K/0K, paused 32ms
FATAL EXCEPTION: main
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) } in anywheresoftware.b4a.phone.PhoneEvents$SMSInterceptor$1@40534828
   at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3691)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.lang.String
   at anywheresoftware.b4a.phone.PhoneEvents$SMSInterceptor$1.onReceive(PhoneEvents.java:362)
   at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
   ... 9 more
Dumpstate > /data/log/dumpstate_app_error
BSmsObserver - onReceive - describeContents = 0
BSmsObserver - action = android.provider.Telephony.SMS_RECEIVED

Any ideas?

Thanks
Martin
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top