在 Android 应用程序中的 REST 服务请求期间显示加载屏幕?

发布于 2024-10-17 21:21:13 字数 515 浏览 1 评论 0原文

目前这是我正在关注的, 我的应用程序一启动,我就必须发送 REST 服务请求, 这需要很少的时间,所以我想到显示加载屏幕,

在我的 Activity 的 onCreate() 中,第一件事是显示加载屏幕(进度对话框), 我使用 AsyncTask 启动后台 Activity,即请求 REST 服务和 onPostexecute() 我关闭对话框,然后执行 setContentView(myxml); 并更新 UI 。

这种方法可以改进吗?

我面临的问题是,

有时,垃圾收集器可能会启动(由于各种原因),并且我的应用程序永远挂在加载屏幕上,因为垃圾收集器,甚至没有发送对 REST 服务的请求,因此会出现一些唤醒呼叫,休息是灾难,力量接近。

但有时即使ForceClose来得也不快,可能是因为GC。所以我什至无法返回并卡在加载屏幕上。那时我唯一能做的就是回家。之后,如果我回到我的应用程序,它仍在加载,所以这种方法显然是一个糟糕的设计。正确的做法是什么?

Currently here is what I am following,
As soon as my app is launched, I have to send a request for REST service,
It will take little time , so I thought of showing loading screen,

In onCreate() of my Activity , first thing will be to show loading screen(progress dialog) ,
And I kick off the background Activity using AsyncTask , i.e. requesting for REST service and onPostexecute() I close the dialog and then I do setContentView(myxml); and update the UI .

Can this approach be improved ?

Problem which I faced was ,

Sometimes , Garbage collector may start(due to various reasons) and my app hangs at loading screen forever , because of Garbage collector , even request for REST service is not sent and because of it some wake up call comes and rest is disaster and Force close.

But sometimes even ForceClose doesnot come fast , may be because of GC. so I cannot even go back and stuck in loading screen. Only thing which I can do at that point is to come back HOME. After that If I come back to my app its still loading , so definitely this approach seems to be a bad design. Whats the right approach ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一瞬间的火花 2024-10-24 21:21:13

如果您预计 REST 调用会花费大量时间,也许您还应该考虑使用服务而不是异步任务。为什么?因为每次方向改变时,您的 Activity 都将被销毁,并且将再次创建新的异步任务,因此从头开始再次执行 REST 调用。另一方面,当使用服务时,您可以发出一次 REST 调用,并且仅跟踪其在 Activity 中的状态(这需要您正确处理 onPause、onCreate 函数)。

我怀疑你的问题与垃圾收集器有任何关系 - 在强制关闭后检查你的堆栈跟踪并尝试了解到底发生了什么。一定是你做错了什么...

If you are expecting your REST call to take significant time, maybe you also should consider using service instead of async task. Why? Because on every orientation change, your activity will be destroyed and new async task will be created again, so the REST call is done again from the start. On the other hand, when using service, you can issue REST call once and only trace it's status in activity (which requires you to properly handle onPause, onCreate functions).

I doubt that your problems have anything to do with garbage collector - check your stack trace after force close and try to understand what really happens. It must be that you are doing something wrong...

甜点 2024-10-24 21:21:13

您可以像我一样实现启动屏幕完成这里

这基本上是启动一个显示启动图像的异步任务,在doInBackground()第51行)中完成工作,然后转发到下一个活动。

You could implement a splash screen like I've done here

This is basically starting an async task that is showing the splash image, doing the work in doInBackground() line 51) and afterwards forwarding to the next activity.

梦里人 2024-10-24 21:21:13

您看过视频 Google I/O 2010 - Android REST 客户端应用程序吗?
在页面上 http://developer.android.com/videos/index.html

Have you watched the video Google I/O 2010 - Android REST client applications?
on the page http://developer.android.com/videos/index.html

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