奥利奥前台服务的正确方式

Oreo Foregroundservice the right way

实现前台服务通知的正确方法是什么。在启动后台线程之前调用通知还是在后台线程中调用它?两种方法都试过了,效果一样,但哪种方法才是正确的?

 @Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    //calling notification before backgroundthread
    runAsForeground();

    Runnable service = new Runnable() {
        @Override
        public void run() {

            //calling notification in backgroundthread
            runAsForeground();
            connect(client,options);

        }
    };

    Thread backgroundThread = new Thread(service);
    backgroundThread.start();
    Log.i(TAG, "onStartCommand methode called");

    return Service.START_NOT_STICKY;
}

Call the notification before starting backgroundthread or calling it in the backgroundthread?

尽快,更准确地说,应用程序必须在创建服务后的五秒内调用服务的 startForeground() 方法。

因此,在您的特定情况下,通过从 Runnable 启动它来延迟调用没有真正的理由或好处。