Android Question SmsInterceptor is not working

pesquera

Active Member
Licensed User
Longtime User
Hi all,

I've updated an app to B4A v8.00, using PhoneLibrary v2.45 to intercept SMS
All is fine on several devices, except on one user having Android 7.1.1

The behavior is weird:
the first SMS it's not intercepted.. after that, all SMS are intercepted Ok
but.. after a few minutes, the situation is the same (first SMS not and later yes)
seems to be that something is shutting the interceptor off

The relevant code:
B4X:
Starter
Service_Create
         StartService(SmsRead)

B4X:
SmsRead
Service_Create
            Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
            Dim GLO_SmsInterceptor As SmsInterceptor
            GLO_SmsInterceptor.Initialize2("GLO_SmsInterceptor", 2147483647)
Service_Start
            Sleep(0) 'allow the MessageReceived event to be raised.
            Service.StopAutomaticForeground

Logging:
1) when the first SMS is arriving, the Starter is fired.. so smsRead is also loaded but SMS is not intercepted
2) when the rest SMS are arriving, the Starter is not fired and the SMS are intercepted Ok.. raising the event "GLO_SmsInterceptor"

I'm not sure about what I'm doing wrong (logic or commands)

Thanks in Advance
 
Last edited:

KMatle

Expert
Licensed User
Longtime User
Upvote 0

pesquera

Active Member
Licensed User
Longtime User
Hi KMatle, I appreciate your help.. that line was there and forgot to copy it.. edited and added to my question's code
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I just use this updated code to intercept with B4A 8.0

Service (s1)

B4X:
Sub Service_Start(startingIntent As Intent)
    If startingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
        SMSList.Initialize
        ParseSmsIntent(startingIntent)
    End if
    
    Sleep(0) 
    Service.StopAutomaticForeground
...

Service_Create is empty

Manifest

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="22"/>
<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(s1, 
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)
 
Upvote 0

pesquera

Active Member
Licensed User
Longtime User
thanks both for that info

I solved this particular issue on my app user backing up some source code (I used StartForeground instead of AutomaticForegroundMode)

the relevant code backed up:
B4X:
SmsRead
Service_Create
       Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
       GLO_NotificationSvc.Icon = "icon"
       GLO_NotificationSvc.Sound = False
       GLO_NotificationSvc.SetInfo("XXX","YYY",Main)
       Service.StartForeground(123456789,GLO_NotificationSvc)
       Dim GLO_SmsInterceptor As SmsInterceptor
       GLO_SmsInterceptor.Initialize2("GLO_SmsInterceptor", 2147483647)
Service_Start
            Sleep(0) 'allow the MessageReceived event to be raised.
            Service.StopAutomaticForeground


Now all is working as I need.. but, I have a confusion about AutomaticForegroundMode vs StartForeground

I've switched AutomaticForegroundMode off and I'm handling the service manually with StartForeground
Should I keep StopAutomaticForeground anyway?

Is there some problem doing that on this way? I mean, about compatibility (devices or Android older/newer versions), resources (battery or cpu), etc
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
The incoming SMS was complete received, i can see the senders numbers and the body (text).
But then the App crashes with following error ( i am using V6.8)

B4X:
MessageReceived: From = +491234567890, Body = This is a test
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x8000010 (has extras) } in anywheresoftware.b4a.phone.PhoneEvents$SMSInterceptor$2@293a410
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:932)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5741)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
    at anywheresoftware.b4a.phone.PhoneEvents$SMSInterceptor$2.onReceive(PhoneEvents.java:398)
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:922)
    ... 7 more

Here is my Manifest content:

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="16" android:targetSdkVersion="23"/>
<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.ACCESS_COARSE_LOCATION)
AddManifestText(<uses-feature android:name="android.hardware.location.gps"/>)
AddApplicationText(
<meta-data
  android:name="com.google.android.geo.API_KEY"
  android:value="secret :-)"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
AddManifestText(<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="23" />
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(s1,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
)
'End of default text.
 
Last edited:
Upvote 0
Top