【问题标题】:How to run active worker in background service?如何在后台服务中运行活动工作者?
【发布时间】:2018-05-24 07:11:19
【问题描述】:

我有一个 Worker Thread 类扩展了 Thread,我在这个类的队列中添加了工作者。我想在后台服务中运行一个工作者。这是我的服务和 onStartCommand 方法

 protected WorkerThread workerThread;
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
     if (this.looperThread.isAlive()) {
        this.looperThread.start(); }
    return Service.START_STICKY;
}

我在我的 MainActivity 中启动服务,但我关闭了应用程序我收到了这样的错误 looperThread.start() Null point Exception。如何在后台服务中运行 worker?谢谢

【问题讨论】:

  • 试过android.app.IntentService?
  • 所以请阅读IntentService 的文档——这是你所需要的

标签: android multithreading service nullpointerexception worker


【解决方案1】:

您需要意向服务。您可以将所需的所有代码放入工作线程中。这是一个例子

public class MyService extends IntentService {
    public MyService() {
        super("Cashback IntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ....Your code that is to be executed in background goes here
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    相关资源
    最近更新 更多