I have a Service
run independently with activity using startService()
. This service handle many requests from activity and create Callable
then add into ThreadPoolExecutor
. It looks like this:
private ExecutorService requestExecutor;
private CompletionService<Result> requestHandleService;
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
// Create new Request Task and submit
Callable<Result> request = new Callable<Result>(){
public Result call() throws Exception {
}
};
requestHandleService.submit(task);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d(TAg,"onDestroy service");
try{
if(requestExecutor!= null){
requestExecutor.shutdown();
}
}catch(Exception ex){
Ln.e(ex);
}finally{
requestExecutor= null;
requestHandleService= null;
}
}
The problem is that I want this Service run independently and parallel with activity. So activity can't control when to stop it. It should only stop when all tasks finished.
I know there is a way to wait for ThreadPool complete, but this can't work for me, because I don't need to keep the list requests. When this service receive request from activity, it should create new a task and submit immediately in onStartCommand
.
Is there any way to solve this?