locked
Android 8+ crash at boot_completed RRS feed

  • Question

  • User365641 posted

    Hi,

    I am currently developing an Android application and I have a mysterious problem on Android 8 to 9. When I restart my phone, I receive an alert message to warn me of the failure of the application (mainly by the service started during Boot_Completed), but the services of the application are still functional. By my stack trace, I can say that the problem comes from the broadcast receiver that launches any other service.

    Unfortunately, I was unable to find a fix to that issue.

    Below, you can see my broadcast receiver.

    [BroadcastReceiver( Name = "com.parroinfo.SERVICE_STARTUP", Enabled = true, Exported = true, DirectBootAware = true)]
        [IntentFilter(new[] {Intent.ActionBootCompleted, Intent.ActionLockedBootCompleted}, Priority = (int)IntentFilterPriority.HighPriority)]
        class ServiceStartup : BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
                {
                 if (AndroidUtils.isVersionBelow(BuildVersionCodes.O))
                 {
                            context.StartService(new Intent(context, -type-));
                         }
                         else
                         {
                            context.StartForegroundService(new Intent(context, -type-));
                         }
            }
        }
    

    Below, you can see my declaration on my manifest.

    <receiver android:exported="true" android:name="com.parroinfo.SERVICE_STARTUP">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    

    Below, you can see the stack trace of the problem.

    java.lang.RuntimeException: 
    at android.app.ActivityThread.handleReceiver (ActivityThread.java:3388)
    at android.app.ActivityThread.access$1200 (ActivityThread.java:199)
    at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1661)
    at android.os.Handler.dispatchMessage (Handler.java:106)
    at android.os.Looper.loop (Looper.java:193)
    at android.app.ActivityThread.main (ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke (Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run  
    (RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858)
    Caused by: java.lang.IllegalStateException: 
    at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1577)
    at android.app.ContextImpl.startService (ContextImpl.java:1532)
    at android.content.ContextWrapper.startService (ContextWrapper.java:664)
    at com.parroinfo.SERVICE_STARTUP.n_onReceive (Native Method)
    at com.parroinfo.SERVICE_STARTUP.onReceive (SERVICE_STARTUP.java:29)
    at android.app.ActivityThread.handleReceiver (ActivityThread.java:3379)
    
    Friday, August 24, 2018 7:37 PM

All replies

  • User21936 posted

    @patriack

    If you have these attributes on your boradcast receiver class:

    [BroadcastReceiver( Name = "com.parroinfo.SERVICE_STARTUP", Enabled = true, Exported = true, DirectBootAware = true)]
    [IntentFilter(new[] {Intent.ActionBootCompleted, Intent.ActionLockedBootCompleted}, Priority = (int)IntentFilterPriority.HighPriority)]
    

    Then you shouldn't need to also add

    <receiver android:exported="true" android:name="com.parroinfo.SERVICE_STARTUP">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    

    To your AndroidManifest.xml file.

    The class attributes will add the above xml elements to the final AndroidManifest.xml file that will be in your final APK package at compile time. So see if the issue persists after you remove the <receiver> element from the AndroidManifest.xml file.

    Friday, August 24, 2018 8:24 PM
  • User365641 posted

    @JGoldberger

    hi, thank for the reply. I removed the element from manifest, but the issue still.

    If you think of something, I'll be glad to hear it.

    Monday, August 27, 2018 12:20 PM
  • User21936 posted

    @patriack

    See if this SO post helps you: https://stackoverflow.com/a/45047542/2913599

    From Google's docs on Android 8.0 behavior changes:

    The system allows apps to call Context.startForegroundService() even while the app is in the background. However, the app must call that service's startForeground() method within five seconds after the service is created.

    Solution: Call startForeground in Oncreate() for the Service which you use Context.startForegroundService().

    See also: Background Execution Limits for Android 8.0 (Oreo)

    Also make sure your changes to the manifest are picked up by the build system by doing a full clean and rebuild of your solution.

    Monday, August 27, 2018 8:03 PM
  • User365641 posted

    Hi, I finally solved my problem. the issue was caused by firebase, who's got an update who avoid to set an boot completed service. So my fix was ??to not start the service for any android version up from marshmallow.

    Tuesday, August 28, 2018 5:13 PM
  • User44328 posted

    Can you please share the code?

    Thursday, June 4, 2020 6:45 AM