处理服务器请求和设备轮换
我读了很多关于在 Android 应用程序中处理旋转的内容,但我仍然有很多问题并且需要了解很多。
让我解释一下我现在在应用程序中使用的问题或实现。
如果要打开一个活动,则会向服务器发送一个获取请求。该请求将在线程(new Thread(...))中执行,如果请求完成,活动的用户界面将被刷新。
但是,如果用户旋转他的设备,我该怎么办?
默认情况下,该 Activity 将被销毁,并重新启动请求并启动一个新线程,但被销毁 Activity 的线程可能仍在运行。
我想,这是一个非常错误的方式,我现在已经这样做了。
但处理这个问题的最佳方法是什么?
可能是禁止轮换的最好方法,但如果我不想要那怎么办?!
可能是我问题的第二部分:
我看到 来自 Google IO 的视频。 Doobjanschi 先生建议使用服务并将检索到的数据存储在内容提供商中。所以,也许我可以使用服务来执行我的请求。但是每次get请求完成后都应该更换数据吗?!
I read a lot about handling rotation in android applications, but I still have so many questions and need to much to understand.
Let me explain my problem or implementation, that I'm using now in my application.
If an activity will be opened, a get request will be sent to server. This request will be executed in a Thread (new Thread(...)) and if request was completed, activity's ui will be refreshed.
But what should I do, if the user rotate his device?
By default, the activity will be destroyed and request will be started again and start a new thread, but the thread of destroyed activity may be still running.
I guess, it's a quite wrong way, I have now.
But what is the best approach, to handle this?
Probably is the best way to forbid rotation, but what If I don't want that?!
May be it's the second part of my question:
I saw a video form Google IO. Mr. Dobjanschi suggested to use services and to store retrieved data in content provider. So, probably I can use a service for executing my requests. But should data be replaced every time the get request was completed?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,不知道它是如何完成的,您可以尝试保存实例并在配置更改时使用以下方法检索相同的实例:
我已经阅读了它们,但还没有真正实现它们。我希望它能给你一些开始。
Well dont know exactly how its done, You can try saving the instance and retrieving the same when config changes with following methods:
I have read about them but haven't really implemented them yet. I hope it can give you some start.
您可以指定活动自行处理旋转。 添加:来完成的
这是通过在 Android 清单内的活动标签中 。您不必实际处理旋转,但这会告诉 android 不要破坏您的活动。基本活动类将为您处理用户界面的所有旋转,并且您的线程将正确执行。
一个小旁注:如果您只执行小型服务器任务,请使用 AsyncTask 在后台执行对服务器的调用,而不是创建线程。这将最大限度地减少将线程结果传达给活动并更新 UI 所需的一些编程工作。
You can specify that the activity handles the rotation itself. This is done through adding:
in the tag of the activity inside your android manifest. You don't have to actually handle the rotation but this will tell android to not destroy your activity. The base activity class will handle all the rotating of the user interface for you and your thread will be executed correct.
A small side note: if you are doing only a small server task use AsyncTask to execute the call to the server in the background instead of creating a thread. This will minimze some of the programming effort you need to communicate the results from the thread to the activity and update your UI.
一种简单的方法,虽然我从未尝试过。当线程完成时,不刷新当前 UI,而是使用刚刚下载的内容启动一个新的 Activity。因此,首先,您使用空白页面(或只是页面框架)启动一个 Activity,然后根据需要旋转空白页面,然后下载线程会生成一个新的 Activity,用加载的内容页面替换空白页面 Activity使用当前方向。
One easy way, though I've never tried it. Instead of refreshing the current UI, when the thread finishes, start a new Activity with the just downloaded content. So first, you start an Activity with a blank page (or just the page's frame), then you rotate the blank page as much as you like, then the downloading Thread spawns a new Activity, replacing the blank page Activity with the loaded content page using the current orientation.