如何等待排球响应完成它在intentservice中的工作? [英] how to wait the volley response to finish it's work inside intentservice?

查看:105
本文介绍了如何等待排球响应完成它在intentservice中的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

您不应该等待它.您有两种发出网络请求的方式:同步和异步.如果使用同步,则不会等待结果,因为网络调用是一项阻止操作.

如果您要遵循这个方向,请参考此内容:我可以做吗与排球同步请求?

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

有关详细信息,请参考以下内容: https://guides.codepath.com/android/Networking-with-凌空图书馆

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

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

解决方案

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.

这篇关于如何等待排球响应完成它在intentservice中的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆