Android Question Crash - java.lang.IllegalStateException - BR.onReceive

fasilosman

Active Member
Licensed User
Longtime User
I have a crash report in google play console.

My targetsdkversion is 26 and B4A version is 8.5.

B4X:
Samsung Galaxy On7 Prime (on7xreflte), Android 8.0
Report 1 of 1
java.lang.RuntimeException:
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:3399)
  at android.app.ActivityThread.-wrap18 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1780)
  at android.os.Handler.dispatchMessage (Handler.java:105)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6942)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
Caused by: java.lang.IllegalStateException:
  at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1538)
  at android.app.ContextImpl.startService (ContextImpl.java:1484)
  at android.content.ContextWrapper.startService (ContextWrapper.java:663)
  at android.content.ContextWrapper.startService (ContextWrapper.java:663)
  at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.startServiceFromReceiver (ServiceHelper.java:125)
  at serandib.jobs.noti$noti_BR.onReceive (noti.java:17)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:3392)

I hope it was from a service. Following are some of the codes from that service

B4X:
Sub Service_Create
    Service.AutomaticForegroundMode= Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED
End Sub

Sub Service_Start (StartingIntent As Intent)
        StartServiceAt(Me, DateTime.Now + 15 * DateTime.TicksPerMinute, False)
        If StartingIntent.HasExtra("android.intent.extra.ALARM_COUNT") Then

            Dim Job As HttpJob
            Job.Initialize("lst",Me)
            Job.Download ("http://xxxx......")
        End If

End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success Then
       '...... raise the notification using NB6
       
    End If
   
    Service.StopAutomaticForeground 

End sub

Please note that I also implemented the crashlytics and it work when I tested in the AVD. But I didn't receive any crash report in firebase console about this error.

Please help me to overcome this crash. Thank you.
 

DonManfred

Expert
Licensed User
Longtime User
What causes you Service to start in Background?
You are NOT using the starterservice for this, right? You shouldn´t use
B4X:
StartServiceAt(Me, DateTime.Now + 15 * DateTime.TicksPerMinute, False)
in the starterservice.
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
What causes you Service to start in Background?
You are NOT using the starterservice for this, right? You shouldn´t use
B4X:
StartServiceAt(Me, DateTime.Now + 15 * DateTime.TicksPerMinute, False)
in the starterservice.

I am using an activity call "Home" to start the service

B4X:
Sub Activity_Create(FirstTime As Boolean)
       '..................
    StartService(Noti)
       '..................
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I am using an activity call "Home" to start the service

Based on the error it looks like your Starter Service get started from a Receiver.
Caused by: java.lang.IllegalStateException:
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:
1538)
at android.app.ContextImpl.startService (ContextImpl.java:
1484)
at android.content.ContextWrapper.startService (ContextWrapper.java:
663)
at android.content.ContextWrapper.startService (ContextWrapper.java:
663)
at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.startServiceFromReceiver (ServiceHelper.java:
125)
at serandib.jobs.noti$noti_BR.onReceive (noti.java:
17)

What are you doing in your notify service?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
But I didn't receive any crash report in firebase console about this error.
The error is from the old version without crashlytics i guess. If i remember correctly you posted a thread about this already and got the suggestion to implement Crashlytics to get a more detailed error. Wait for the Crashlytics report.

Did you, after implementing Crashlytics, create a crash to test if the implementation is working before you publish the new version to the playstore?
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
Based on the error it looks like your Starter Service get started from a Receiver.


What are you doing in your notify service?

starter service have the following code only.
B4X:
Sub Service_Create

    Dim context As JavaObject 'depends on JavaObject
    context.InitializeContext
    CL.Initialize(context)

End Sub

The code in #1 is from the notification service. Call httpjob and in jobdone I check the result if there is a new id then raise a notification.
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
The error is from the old version without crashlytics i guess. If i remember correctly you posted a thread about this already and got the suggestion to implement Crashlytics to get a more detailed error. Wait for the Crashlytics report.

Did you, after implementing Crashlytics, create a crash to test if the implementation is working before you publish the new version to the playstore?

No, this is from new version (verions 5 ) which I have implemented crashlytics.
 

Attachments

  • err.png
    err.png
    21 KB · Views: 196
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
I just received a crashlytics report from firebase console. The error is from the same device and the same app versions. Also looks like the same error.
Could anyone please find information from the report.

err1.png
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Have you tested your app on an Android 8+ device? Are you able to reproduce this issue?

You can change Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED to Service.AUTOMATIC_FOREGROUND_ALWAYS. It will probably solve this issue.

It seems like the framework thinks that the app is in the foreground while it is actually in the background.
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
Have you tested your app on an Android 8+ device? Are you able to reproduce this issue?

You can change Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED to Service.AUTOMATIC_FOREGROUND_ALWAYS. It will probably solve this issue.

It seems like the framework thinks that the app is in the foreground while it is actually in the background.
Yes I checked in 8+ devices, couldn’t reproduce.

I will do the changes. Thank you Erel.
 
Upvote 0
Top