Context.startForegroundService did not then call S

2019-07-31 15:29发布

Note: This question assumes you know binding a service to a context.

Everyone on the production is aware of this issue, or even some users that have an Android Oreo or Pie device. The exception looks like this:

Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:164)
   at android.app.ActivityThread.main(ActivityThread.java:6523)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)

When you call Context.startForegroundService(Intent) you must also call Service.startForeground(int, Notification) within 5 seconds (even though your phone maybe can't even start your service, it becomes your fault for some reason) or your app will freeze or throw an exception from Android framework. Upon further thinking, I've developed a solution.

Since Context.startForegroundService(Intent) does not assure that the service is going to be created within 5 seconds and is an asynchronous operation, I've thought something goes along this way:

// Starts service.
public static void startService(Context context)
{
    // If the context is null, bail.
    if (context == null)
        return;

    // This is the same as checking Build.VERSION.SDK_INT >= Build.VERSION_CODES_O
    if (Utilities.ATLEAST_OREO)
    {
        Log.w(TAG, "startForegroundService");

        // Create the service connection.
        ServiceConnection connection = new ServiceConnection()
        {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service)
            {
                // The binder of the service that 
                // returns the instance that is created.
                MyService.LocalBinder binder = (MyService.LocalBinder) service;

                // The getter method to acquire the service.
                MyService myService = binder.getService();

                // getServiceIntent(context) returns the relative service intent.
                context.startForegroundService(getServiceIntent(context));

                // This is the key: Without waiting Android Framework 
                // to call this method inside Service.onCreate(), 
                // immediately call here to post the notification.
                // MyService.createNotification(context) is static
                // for other purposes.
                myService.startForeground(19982, MyService.createNotification(context));

                // Release the connection to prevent leaks.
                context.unbindService(this);
            }

            @Override
            public void onServiceDisconnected(ComponentName name)
            {

            }
        };

        // Try to bind the service.
        try
        {
            context.bindService(getServiceIntent(context), connection, Context.BIND_AUTO_CREATE);
        }
        catch (RuntimeException ignored)
        {
            // This is probably a broadcast receiver context 
            // even though we are calling getApplicationContext().
            // Just call startForegroundService instead 
            // since we cannot bind a service to a
            // broadcast receiver context. 
            // The service also have to call startForeground in this case.
            context.startForegroundService(getServiceIntent(context));
        }
    }
    else
    {
        // Normal stuff below API 26.
        Log.w(TAG, "startService");
        context.startService(getServiceIntent(context));
    }
}

This function is called from an activity that starts the service. The interesting thing is: it works and does not throw an exception. I've tested on emulators, my device which is also Oreo, and I want to confirm that this is the best approach to prevent this exception from happening.

My question is: Is this a good way to actually create a foreground service? Any thoughts? Thank you for your comments.

0条回答
登录 后发表回答