IntentService自己的Thread

IntentService's own Thread

我知道 IntentService 本身 运行 在不同的线程上。

此外,它还会执行 onHandleIntent() 并在该方法完成后停止。

我的问题是:在 Intent 服务中创建我自己的自定义线程会产生任何后果吗?

我知道它可以在服务中完成,但我想知道这是否是使用 IntentService 的错误方式

要获得更多信息,我需要做的是发送大量 HTTP 请求。

我要做的是将请求字符串和执行它们的 运行 意图服务保存在数据库中。 这就是我使用 IntentService 的原因,请求可能需要时间,我希望服务在包含请求的 table 为空后关闭。

我想我可以通过添加我自己的线程来提高此服务的速度,因为我将 运行宁可以说,每次 5 个线程。

编辑: 这是我想做的代码,我想它会清楚我正在尝试做的事情,如果可能的话。

 protected void onHandleIntent(Intent intent) {
    helper = new DBHelper(getApplicationContext());
    File file;
    //checks if the DB requests exists
    while(helper.requestsExists()){
        ArrayList<String> requestArr = helper.getRequestsToExcute(5);
        if(!requestArr.isEmpty()){
            //execute them and delete the DB entry
            for(int i=0;i<requestArr.size();i++){
                file = new File(requestArr.get(i));
                new MyThread(file).start();// the DB entry is delete withing the thread
            }
        }
    }
}

所以这个服务会 运行 只要它在我的 SQLite 数据库上有任何数据库条目,在它执行完所有这些条目后它就会停止。 可以吗,还是我应该使用服务?

正如您所解释的,onHandleIntent 是在工作线程上调用的,带有要处理的请求。一次只处理一个 Intent,但处理发生在独立于其他应用程序逻辑运行的工作线程上。

为什么要创建一个单独的线程,除非您想处理多个传入的请求并并行处理它们?

因此,如果您处理您在 onHandleIntent 中建议的操作,它将阻止对同一 IntentService 的其他请求,但不会阻止任何其他请求。

这个答案可能对您有所帮助 - start async task from onhandleintent