Android应用程序中的网络服务
我的应用程序中有一个网络服务,目前它是应用程序上下文中的线程还是服务并不重要,重要的是我无法正确获得响应。
该服务正在工作,并且我确实得到了响应,现在我正在使用观察者设计模式,这是 android 中的一个混蛋:-( 我必须在销毁时删除侦听器,然后检查网络是否已全部准备好完成并且处理不同的场景。
我想改变我的方法,但我不确定什么是正确的android方法,似乎活动是为了轮询而进行的,这对我的架构不利并且容易出错:-(。
我想到了以下两种方法,但似乎没有给我一个真正的解决方案:
- 创建一个广播接收器 如果满足以下条件,将发送带有标志的广播包: 响应或错误来自 net,它适合于活动 轮询正确的数据并处理。我愿意 想知道这个,因为如果我 注册广播我基本上 必须删除监听器 暂停,这样我就可以错过广播, 然后又回来查网 onCreate 中的状态,与 我现在正在做 - 糟透了...
- 注册一个活动,这是什么事件,并使用 startActivity 向它发送一个意图,但在这里,如果该活动处于暂停或停止状态,我会将其带到前面,我不想要这个一种行为,我只希望该活动无论如何都能获得事件,并开始想知道是否有办法做到这一点。
风景/肖像事件快要了我的命,有什么办法摆脱它吗?
I have a net service in my app, for now it doesn't matter if it's a thread in the Application context or a service, what does matter is i have some trouble getting response properly.
The service is working, and i do get responses, right now i work with the observer design patter which is a bitch in android :-( i have to remove the listener in on destroy and then check the network if it is all ready finished and handle different scenarios.
I want to change my approach, but i'm not sure what's the right android way, it seems like activities are made for polling which is bad for my architecture and error prone :-(.
I thought of the folowing two methods but non seem to give me a real solution:
- create a braodcast receiver that
will send a broadcase with a flag if
response or error arrived from the
net, it's apt to the activity to
poll the right data and handle. i do
wonder about this since if i
register for broacast i basically
have to remove the listener in on
pause, so i CAN miss a broadcast,
then it's back to check the net
state in onCreate, same like what
i'm doing now - sucks... - register the activity that whats an event and sent it an intent using startActivity but here if the activity is in on pause or stop i'll bring it to the front, i don't want this kind of behavior, i only want that the activity will get the event no matter what, and starting to wonder if there's a way to do that.
The landscape\portrait event are killing me, any way out of it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了以下内容:
我自己和风景\肖像事件
然后我消除了90%的问题
如果我得到不会杀死我的应用程序的新意图(取决于意图),我会在 onNewIntent() 方法中得到它。我可以将一些依赖于这些值的初始化函数放在一个单独的初始化函数中,该函数将它们全部收集起来以在 onCreate 和 onNewIntnet 之间共享它们。
i discovered the following:
landscape\portrait events myself and
then i eliminate 90% of the problem
if i get new intent that doesn't kill my app (depends on the intent) i get it in onNewIntent() method. i can put some init functions that are dependant on those values in a seperate init function that gathers them all to share them between onCreate and onNewIntnet.