Android应用程序中的网络服务

发布于 2024-10-10 05:32:10 字数 637 浏览 2 评论 0原文

我的应用程序中有一个网络服务,目前它是应用程序上下文中的线程还是服务并不重要,重要的是我无法正确获得响应。
该服务正在工作,并且我确实得到了响应,现在我正在使用观察者设计模式,这是 android 中的一个混蛋:-( 我必须在销毁时删除侦听器,然后检查网络是否已全部准备好完成并且处理不同的场景。
我想改变我的方法,但我不确定什么是正确的android方法,似乎活动是为了轮询而进行的,这对我的架构不利并且容易出错:-(。

我想到了以下两种方法,但似乎没有给我一个真正的解决方案:

  1. 创建一个广播接收器 如果满足以下条件,将发送带有标志的广播包: 响应或错误来自 net,它适合于活动 轮询正确的数据并处理。我愿意 想知道这个,因为如果我 注册广播我基本上 必须删除监听器 暂停,这样我就可以错过广播, 然后又回来查网 onCreate 中的状态,与 我现在正在做 - 糟透了...
  2. 注册一个活动,这是什么事件,并使用 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:

  1. 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...
  2. 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 技术交流群。

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

发布评论

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

评论(1

2024-10-17 05:32:10

我发现了以下内容:

  1. 我可以决定处理
    我自己和风景\肖像事件
    然后我消除了90%的问题
  2. ,我可以使用广播接收器,但是如果有粘性标志,这将导致意图等待我,我只需要首先检查我是否得到了意图,一旦它是粘性的,我会再次GT它(意思是在横向\纵向中,我可能会在屏幕旋转时得到它两次)

如果我得到不会杀死我的应用程序的新意图(取决于意图),我会在 onNewIntent() 方法中得到它。我可以将一些依赖于这些值的初始化函数放在一个单独的初始化函数中,该函数将它们全部收集起来以在 onCreate 和 onNewIntnet 之间共享它们。

i discovered the following:

  1. i can decide to handle the
    landscape\portrait events myself and
    then i eliminate 90% of the problem
  2. i can use Broadcast Receivers but with sticky flag on that will cause the intent to wait for me, i just have to check first if i got the intent once i'll gt it once more if it's sticky (meaning in landscape \ portrait i may get it twice on screen rotation)

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.

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