如何等待截击响应完成它在 intentservice 中的工作?

how to wait the volley response to finish it's work inside intentservice?

使用 intentservice 在后台使用“Google Volley”获取 7 个 Rss Feed 链接的数据并使用 ResultReceiver 获取结果,但我无法配置如何等待 volley响应完成它的工作,用 ResultReceiver 触发标志以在 MainActivity

中显示数据

你不应该等待它。您可以通过两种方式发出网络请求:同步和异步。如果使用同步,则不需要等待结果,因为网络调用是阻塞操作。

如果你想往这个方向走,请参考这个:Can I do a synchronous request with volley?

如果你想让它异步,你只需启动一个请求,然后在请求完成后在 Response.Listener 回调方法中使用你的 ResultReceiver

详情请参考: https://guides.codepath.com/android/Networking-with-the-Volley-Library

如果您仍然认为应该阻塞当前线程并等待结果,您应该使用CountDownLatch。创建一个后台线程(你不能在 android 中阻塞 UI 线程,除非它是一个单独的进程),用计数 1 初始化你的闩锁并调用 await() 方法。这将阻塞你的后台线程,直到计数为 0。一旦你的后台任务完成,你调用 countDown() 这将解锁你的后台线程,然后你将能够执行你的操作。

此问题已在 official Volley Google Group 中得到回答。

将请求包装在 RequestFuture 中以使用 RequestFuture#newFuture(...);

进行阻塞调用

您可以在 Volley source code:

中找到示例代码
RequestFuture<SONObject> future = RequestFuture.newFuture();
MyRequest request = new MyRequest(URL, future, future);

// If you want to be able to cancel the request:
future.setRequest(requestQueue.add(request));

// Otherwise:
requestQueue.add(request);

try {
  JSONObject response = future.get();
  // do something with response
} catch (InterruptedException e) {
  // handle the error
} catch (ExecutionException e) {
  // handle the error
}

请务必在 future.get(...) 中使用超时,否则您的线程将锁定。