代码之家  ›  专栏  ›  技术社区  ›  TsigumEnes

如何在后台服务中运行活动工作人员?

  •  -2
  • TsigumEnes  · 技术社区  · 6 年前

    我有一个 工作线程 类扩展线程,并在此类的队列中添加工作线程。我希望在后台服务中运行一个工作线程。这是我的服务和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;
    }
    

    我在我的主活动中启动了服务,但我关闭了应用程序,出现了这样的错误 looperthread.start()空点异常 .如何在后台运行工人?谢谢

    1 回复  |  直到 6 年前
        1
  •  0
  •   Suhaib Roomy    6 年前

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

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