活动和线程处理
我想在 onCreate()
中发出 http 请求(我使用 ksoap2),在此期间我希望屏幕上有一个 ProgressDialog
来阻止用户。请求完成后,我想使用从 http 请求中检索的元素来完成 onCreate。
现在:
我有一个实现 Runnable 的类。我在 onCreate 中实例化它。该类发出 http 请求,此时屏幕上会出现一个 ProgressDialog。完成后,ProgressDialog 将被关闭。但由于它不在 UI 线程中,因此在获取 http 请求的结果之前会执行以下 onCreate 操作。
首先,我也尝试过:在 onCreate 中,我对 ProgressDialog 执行 show()
操作,然后执行 http 请求,然后执行 dismiss()
ProgressDialog 操作,最后使用以下命令完成 onCreate我的http请求的结果...除了ProgressDialog这次没有显示之外,一切都还好。
抱歉解释混乱,我的英语也没什么帮助。
无论如何,想法?
谢谢。
PS:顺便说一句,我知道 AsyncTask 更好,这是我通常使用的,但是将它与 ProgressDialog 一起使用并不容易。
我的问题更多:在 onCreate()
中是否有一种方法可以发出 http 请求来获取 int
的值。发出此请求时:显示一个 ProgressDialog
来阻止用户,同时也阻止 UI 线程,以便 onCreate 的末尾可以使用 http 请求完成后检索到的值。
一种丑陋的方式是在 onCreate 中循环并休眠等待检索值......这真的很丑陋。
I want to make an http request (I use ksoap2) in the onCreate()
during this time I want to have a ProgressDialog
on screen to block the user. Once the request is done, I want to finish my onCreate using the elements that I retrieve from my http request.
For now:
I have a class which implements Runnable. I instantiate it in my onCreate. This class make the http request, during this time a ProgressDialog is on the screen. Once it's done the ProgressDialog is dismissed. But since it's not in the UI thread the following of the onCreate is executed before getting the result of the http request.
I also tried, at first: in the onCreate I do a show()
of my ProgressDialog then the http request then a dismiss()
ProgressDialog and finally finish my onCreate with the result of my http request... it's ok except that the ProgressDialog is not showing this time.
Sorry for the messy explanations, my english is really not helping either.
Anyway, ideas ?
Thanks.
PS : BTW I know AsyncTask is better, it is what I usually use but having it with a ProgressDialog is not at all easy.
My question is more : is there a way in an onCreate()
to make an http request to get the value of an int
. While this request is made: showing a ProgressDialog
to block the user but also block the UI thread so that the end of the onCreate can use the value retrieved by the http request once it is complete.
An ugly way would be a loop in the onCreate with a sleep waiting for the value to be retrieved... that's really ugly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:我知道您提到您不想使用
AsyncTask
由于显示ProgressDialog
的复杂性,但除非我错过了一些东西,否则它应该像以下。或者,您可以使用处理程序来触发回调?我认为您需要某种处理程序来在
HTTP
请求完成后进行回调,然后在回调中完成您的代码。我还使用
ksoap2
进行 Web 服务调用,并在调用时显示ProgressDialog
。 Web 服务器调用完成后,它会引发一个事件,然后我关闭ProgressDialog
。我不确定这是否会对您有帮助,但我有以下方法(当然,可能有更好的方法,但这对我有用),这样在 UI 线程上调用ProgressDialog
,一个AsyncTask
完成并调用完整方法(该方法将线程返回到 UI),然后我关闭该对话框:编辑:我认为您要问的是
onCreate
(从 UI 线程运行),你想显示一个ProgressDialog
并在同一方法中从 HTTP 请求返回一个值,一旦完成,就隐藏对话框。然后你说你已经尝试过,但
ProgressDialog
没有显示。这是因为您使用 HTTP 请求阻塞了 UI 线程。如果您从
onCreate
方法调用 HTTP 请求,则必须在单独的线程中运行它(或者像您所做的那样,实现Runnable
)和一个将调用 runnable 的处理程序然后您可以使用它处理返回值。简而言之,您不能:
Show
ProgressDialog
> >调用HTTP请求>获取值>隐藏ProgressDialog
而不为 HTTP 请求添加另一个线程(或者ProgressDialog
将不会显示),这意味着您的返回 HTTP 请求代码必须位于调用之外onCreate
方法。Edit: I know you mentioned you didn't want to use
AsyncTask
due to the complexities of showing theProgressDialog
, but unless I'm missing something it should be as simple as the below. Alternatively, you could use a handler to fire the call back?I think you need some kind of handler to callback once the
HTTP
request is done, and then in the callback finish off your code.I also use
ksoap2
for a web service call and show aProgressDialog
whilst it's called. Once the web server call is finished, it raises an event and I close theProgressDialog
. I'm not sure if this will help you, but I have the following (of course, there may be better ways, but this works for me), this way theProgressDialog
is called on the UI thread, anAsyncTask
completes and calls the complete method (which returns the thread back to the UI) and I dismiss the dialog:Edit: I think what you're asking is
onCreate
(ran from the UI thread), you want to show aProgressDialog
and in the same method return a value from an HTTP request and once it completes, hide the dialog.You then say you've tried that and the
ProgressDialog
does not show. This is because you block the UI thread with the HTTP request.Providing you call the HTTP request from the
onCreate
method, you must run it in a separate thread (or as you done, implementRunnable
) and a handler which will call the runnable with which you can then handle the return value.Put simply, you can't:
Show
ProgressDialog
> Call HTTP request > Get value > HideProgressDialog
without adding another thread for the HTTP request (or theProgressDialog
will not show) which in turn means your return HTTP request code must be outside of the callingonCreate
method.在您的
oncreate
中首先显示对话框
,然后创建一个线程
并在完成后执行您的http任务
从线程中调用处理程序
并关闭
您的对话框。In your
oncreate
showdialog first
, then create athread
and do yourhttp task
there and once you are done with that call thehandler
from the thread anddismiss
your dialog.