首页 > 解决方案 > 奥利奥前台服务的正确方式

问题描述

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

 @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;
}

标签: androidandroid-serviceforeground-service

解决方案


在启动后台线程之前调用通知还是在后台线程中调用它?

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

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


推荐阅读