Context.startForegroundService没有调用Service.startForeground:替代方案

5

注意:本问题假定您知道如何将服务绑定到上下文。

生产环境中的每个人都知道这个问题,甚至一些使用安卓Oreo或Pie设备的用户也知道。异常信息看起来像这样:

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)

当您调用Context.startForegroundService(Intent)方法时,必须在5秒内调用Service.startForeground(int, Notification)方法(即使您的手机可能无法启动您的服务,由于某种原因导致这成为了您的问题),否则您的应用程序将会被冻结或从Android框架中抛出异常。经过进一步思考,我想到了一个解决方案。

由于Context.startForegroundService(Intent)方法不能保证服务会在5秒内被创建,并且是一个异步操作,我考虑采取以下方式:

// 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));
    }
}

这个函数从启动服务的活动中调用。有趣的是:它起作用并且不会抛出异常。我已经在模拟器、我的设备(也是 Ore)上测试过,我想确认这是防止此异常发生的最佳方法。

我的问题是:这是实际创建前台服务的好方法吗?有什么想法吗?感谢你的评论。


你在 AndroidManifest 文件中添加了 FOREGROUND_SERVICE 权限吗? - Nikunj Sorathiya
当然可以。这不是问题。 - Furkan Yurdakul
大家有什么想法吗?我认为这是一个重要的案例。 - Furkan Yurdakul
1个回答

0
根据codelab,这似乎是在Android 9.0+上实现前台服务的正确方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接