Android Question Crash reports - ForegroundServiceStartNotAllowedException and ClassCastException

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I got 2 crash reports yesterday and have no idea where to start looking for them from 2 different devices

This one seems to be saying something about startForegroundService but in B4XPages server I only have Service.StopAutomaticForeground

B4X:
Type java.lang.RuntimeException

Exception java.lang.RuntimeException:
  at anywheresoftware.b4a.keywords.Common$11.run (Common.java:1183)
  at android.os.Handler.handleCallback (Handler.java:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loopOnce (Looper.java:226)
  at android.os.Looper.loop (Looper.java:313)
  at android.app.ActivityThread.main (ActivityThread.java:8855)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)
Caused by android.app.ForegroundServiceStartNotAllowedException:
  at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:54)
  at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:50)
  at android.os.Parcel.readParcelable (Parcel.java:3346)
  at android.os.Parcel.createExceptionOrNull (Parcel.java:2433)
  at android.os.Parcel.createException (Parcel.java:2422)
  at android.os.Parcel.readException (Parcel.java:2405)
  at android.os.Parcel.readException (Parcel.java:2347)
  at android.app.IActivityManager$Stub$Proxy.startService (IActivityManager.java:7026)
  at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1935)
  at android.app.ContextImpl.startForegroundService (ContextImpl.java:1901)
  at android.content.ContextWrapper.startForegroundService (ContextWrapper.java:798)
  at anywheresoftware.b4a.keywords.Common.StartServiceImpl (Common.java:904)
  at anywheresoftware.b4a.keywords.Common.StartService (Common.java:892)
  at anywheresoftware.b4a.keywords.Common$11.run (Common.java:1153)

This one appears to be happening in b4xpages - not something I edit?
B4X:
Type  java.lang.ClassCastException

Exception java.lang.ClassCastException:

  at com.BOBs.BBS.b4xpages._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv6 (b4xpages.java:78)
  at com.BOBs.BBS.b4xpages._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv5 (b4xpages.java:147)
  at com.BOBs.BBS.cconsent._email_click (cconsent.java:169)
  at com.BOBs.BBS.cconsent._consent_webview_overrideurl (cconsent.java:126)
  at java.lang.reflect.Method.invoke
  at anywheresoftware.b4a.BA.raiseEvent2 (BA.java:213)
  at anywheresoftware.b4a.BA.raiseEvent (BA.java:193)
  at anywheresoftware.b4a.objects.WebViewWrapper$1.shouldOverrideUrlLoading (WebViewWrapper.java:64)
  at android.webkit.WebViewClient.shouldOverrideUrlLoading (WebViewClient.java:83)
  at org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading (chromium-TrichromeWebViewGoogle.aab-stable-<US_SOCIAL_SECURITY_NUMBER>:89)
  at android.os.MessageQueue.nativePollOnce
  at android.os.MessageQueue.next (MessageQueue.java:335)
  at android.os.Looper.loopOnce (Looper.java:186)
  at android.os.Looper.loop (Looper.java:313)
  at android.app.ActivityThread.main (ActivityThread.java:8751)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)
 

JohnC

Expert
Licensed User
Longtime User
Could it have to do with the new Android 12 restrictions on services?


Even though you are immediately using "Service.StopAutomaticForeground", it is still briefly starting as a foreground service by default and thus genarating the error.
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I'm not using any Services (other than the default starter service that gets created in a B4XPages program and am only using it to trap the Applicaton_Error)

I think I see the problem with the cconsent.email_click

B4X:
Public  Sub EMail_Click(Tag As Object)
            Log("EMail Selected " &Tag)    
            
            B4XPages.ShowPage(cBBsGlobals.DEFINE_PageName_ContactSupport)
            
            sConsent_Accept_Click
End Sub

I am showing a different page and then processing as if the user pressed the Accept button on my (another button on page) probably need to process it in reverse order
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
If you are not using a service, then why did you say "I only have Service.StopAutomaticForeground"?
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Sorry. This has been in my Starter service since ????
B4X:
Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
   
    logs.Initialize
           
'#if Release
    logcat.LogCatStart(Array As String("-v","raw","*:F","B4A:v"), "logcat")
'#end if
   
End Sub
 
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Wish I could answer that - there is no reason my app should start anyway but from clicking on the icon.

I have no receivers or services in my manifest or app (other than Starter)

I was wondering if maybe the user (who I do not know who is) maybe was using a task launcher? Is that possible.

Guess I will have to wait until it happens again (if it does)
 
Upvote 0
Top