使用intentservice在后台使用“Google Volley”获取7 Rss Feed链接的数据并使用ResultReceiver获取结果,但是我无法配置如何等待排队响应来完成它的工作ResultReceiver显示MainActivity中的数据
Working with intentservice to get the data of 7 Rss Feed links with using " Google Volley " in the background and use ResultReceiver to get the result , But I can't configure how to wait on the volley response to finish it's job to fire flag with ResultReceiver to display the data in the MainActivity
最满意答案
你不应该等待它。 有两种方法可以发出网络请求:同步和异步。 如果使用同步,则不会等待结果,因为网络呼叫是阻止操作。
如果你想要这个方向,请参考这个: 我可以用齐射进行同步请求吗?
如果你想使其成为异步的,那么你只需要开始一个请求,然后在请求完成后使用你的ResultReceiver在Response.Listener回调方法中。
有关详细信息,请参阅此处: https : //guides.codepath.com/android/Networking-with-the-Volley-Library
如果您仍然认为应该阻止当前线程并等待结果,则应使用CountDownLatch 。 创建一个后台线程(你不能在android中阻塞UI线程,除非它是一个单独的进程),用count 1初始化你的锁存器并调用await()方法。 这将阻止你的后台线程,直到计数为0.一旦你的后台任务完成,你可以调用countDown()来解除后台线程的阻塞,然后你就可以执行你的动作了。
You shouldn't wait for it. You have two ways to make a network request: synchronous and asynchronous. If you use synchronous, you don't wait for result because the network call is a blocking operation.
Refer this if you want to go this direction: Can I do a synchronous request with volley?
If you want to make it asynchronous, you just start a request and then use your ResultReceiver in a Response.Listener callback method once the request is finished.
Refer this for the details: https://guides.codepath.com/android/Networking-with-the-Volley-Library
If you still think you should block the current thread and wait for result, you should use CountDownLatch. Create a background thread (you cannot block UI thread in android unless it's a separate process), init your latch with count 1 and call await() method. This will block your background thread till the count is 0. Once your background task is finished you call countDown() which will unblock your background thread and you will be able to perform your action then.
更多推荐
发布评论