活动和线程处理

发布于 2024-12-07 23:44:03 字数 895 浏览 0 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

り繁华旳梦境 2024-12-14 23:44:03

编辑:我知道您提到您不想使用 AsyncTask 由于显示 ProgressDialog 的复杂性,但除非我错过了一些东西,否则它应该像以下。或者,您可以使用处理程序来触发回调?

我认为您需要某种处理程序来在 HTTP 请求完成后进行回调,然后在回调中完成您的代码。

我还使用 ksoap2 进行 Web 服务调用,并在调用时显示 ProgressDialog。 Web 服务器调用完成后,它会引发一个事件,然后我关闭 ProgressDialog。我不确定这是否会对您有帮助,但我有以下方法(当然,可能有更好的方法,但这对我有用),这样在 UI 线程上调用 ProgressDialog ,一个 AsyncTask 完成并调用完整方法(该方法将线程返回到 UI),然后我关闭该对话框:

    // Notify user the web service is connecting
    final ProgressDialog dialog = ProgressDialog.show(
            Screen_GlobalLeaderboard.this, "Getting scores",
            "Please wait...", true);
    dialog.setCancelable(true);

    WebService ws = new WebService(this) {

        @Override
        public void webServiceCallComplete(boolean success, Object data,
                String errorMessage, WebMethods callingWebMethod) {


            dialog.dismiss();

            // Your complete code here
        }
    };

    ws.getTopLeaderboardResults(top, gameMode);

编辑:我认为您要问的是 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 the ProgressDialog, 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 a ProgressDialog whilst it's called. Once the web server call is finished, it raises an event and I close the ProgressDialog. 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 the ProgressDialog is called on the UI thread, an AsyncTask completes and calls the complete method (which returns the thread back to the UI) and I dismiss the dialog:

    // Notify user the web service is connecting
    final ProgressDialog dialog = ProgressDialog.show(
            Screen_GlobalLeaderboard.this, "Getting scores",
            "Please wait...", true);
    dialog.setCancelable(true);

    WebService ws = new WebService(this) {

        @Override
        public void webServiceCallComplete(boolean success, Object data,
                String errorMessage, WebMethods callingWebMethod) {


            dialog.dismiss();

            // Your complete code here
        }
    };

    ws.getTopLeaderboardResults(top, gameMode);

Edit: I think what you're asking is onCreate (ran from the UI thread), you want to show a ProgressDialog 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, implement Runnable) 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 > Hide ProgressDialog without adding another thread for the HTTP request (or the ProgressDialog will not show) which in turn means your return HTTP request code must be outside of the calling onCreate method.

莫言歌 2024-12-14 23:44:03

在您的 oncreate 中首先显示对话框,然后创建一个线程并在完成后执行您的http任务从线程中调用处理程序关闭您的对话框。

In your oncreate show dialog first, then create a thread and do your http task there and once you are done with that call the handler from the thread and dismiss your dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文