stefanosn
stefanosn

Reputation: 3324

I get this crash from google developers dashboard but i cant understand what is causing it

This is the crash i get in google play developer dashboard. Rebootreceiver extends broadcastreceiver class where onReceive() i call another class that extends service where in that class i call a function to start an alarm. Could this crash caused because google alarms are disabled for my app by the user or something similar? Any thoughts?

java.lang.RuntimeException: 
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4238)
  at android.app.ActivityThread.access$1700 (ActivityThread.java:274)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2113)
  at android.os.Handler.dispatchMessage (Handler.java:107)
  at android.os.Looper.loop (Looper.java:237)
  at android.app.ActivityThread.main (ActivityThread.java:8167)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:496)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1100)
Caused by: java.lang.IllegalStateException: 
  at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1688)
  at android.app.ContextImpl.startService (ContextImpl.java:1633)
  at android.content.ContextWrapper.startService (ContextWrapper.java:683)
  at android.content.ContextWrapper.startService (ContextWrapper.java:683)
  at com.example.myapp.RebootReceiver.onReceive (RebootReceiver.java:31)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4229)
  at android.app.ActivityThread.access$1700 (ActivityThread.java:274)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2113)
  at android.os.Handler.dispatchMessage (Handler.java:107)
  at android.os.Looper.loop (Looper.java:237)
  at android.app.ActivityThread.main (ActivityThread.java:8167)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:496)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1100)

Upvotes: 0

Views: 53

Answers (1)

Amani
Amani

Reputation: 3977

From your stack trace i find out that your receiver is RebootReceiver when device starts your app is in background. If your app targeting API is 26 (Android 8) or above and it tries to start a service when it is in background it will crash with IllegalStateException so you need to call startForgroundService if Android is 8 or above instead of startService:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(new Intent(context, ServedService.class));
} else {
    context.startService(new Intent(context, ServedService.class));
}

And in onHandleIntent of service you need to call startForeground otherwise you get IllegalStateException again.

If you have set targetSdkVersion = 28 (Android 9 / Pie) or above to start foreground service you need to declared the usage of the FOREGROUND_SERVICE permission:

<manifest ...>
     ...
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     ...
     <application ...>
     ...
</manifest>

Upvotes: 1

Related Questions