服务结构、HttpClient 使用和 ANR

发布于 2025-01-08 04:15:06 字数 995 浏览 1 评论 0原文

好吧,让我解释一下我在做什么,然后我有几个问题。

说明

我正在开发一个应用程序,该应用程序将使用一项服务每 1-10 分钟检查我的网页是否有更新。因此,在 ACTION_BOOT_COMPLETED 上,MyReceiver(Broadcast Receiver) 被触发,如果它是 ACTION_BOOT_COMPLETED 广播,它将启动一个名为 BootService 的服务。然后BootService设置一个AlarmService来触发MyReceiver,然后BootService被终止。当警报广播进入 MyReceiver 时,它会触发另一个名为 UpdateService 的服务,该服务将以 BootService 设置的任何时间间隔检查互联网。

我有一个名为 BootService 的服务,因为每次启动我的应用程序时,它都会启动 BootService 以确保 AlarmService 正在运行,以防它因任何原因终止。

当我发出 HTTPClient 请求时,我希望它受到登录保护,因此我试图做的是每当 BootService 运行时,它都会向我的网站发起 HTTPClient 请求,登录并存储我将在 UpdateService 中使用的会话 ID。但是,当我将 HTTPClient 请求放入 BootService 时,我收到 ANR(应用程序未响应)。我尝试将 HTTPClient 请求放入 AsyncTask 中,但仍然遇到问题。

问题

  1. 如果我取消 BootService 而只让 MyReceiver 设置 AlarmService 会更好吗?每当有人加载应用程序时,都会再次发出广播来启动警报?

  2. 什么可能导致 HTTPClient 请求期间的所有阻塞,从而锁定我的程序并强制 ANR?我怎样才能正确地实现我的 HTTPClient 请求,这样它就不会阻止我的服务?

  3. 登录/cookie 存储方法的任何好的示例。以及稍后使用会话 ID 发出未来请求的服务?

Alright so let me explain what I'm doing and then I have a couple questions.

Explination

I have an app I'm working on that will use a service to check my webpage for updates every 1-10 minutes. So on ACTION_BOOT_COMPLETED, MyReceiver(Broadcast Receiver) is triggered and if it's an ACTION_BOOT_COMPLETED broadcast it starts a service called BootService. BootService then sets an AlarmService that triggers MyReceiver, and then BootService is terminated. When an alarm broadcast comes into MyReceiver it triggers another service called UpdateService which will check the internet at whatever interval that is set by BootService.

I have a service called BootService because everytime my app is started, it starts up BootService to make sure the AlarmService is running just in case it gets terminated for whatever reason.

When I make my HTTPClient requests I want it to be login protected so what I was trying to do is whenever BootService is run, it will initiate a HTTPClient request to my website, login and store a session id that I'll use in UpdateService. However, when I put the HTTPClient request into BootService I get a ANR (Application Not Responding). I tried putting the HTTPClient request into an AsyncTask and still I have issues with it.

Questions

  1. Would it be better if I did away with the BootService and instead just have MyReceiver set an AlarmService. And whenever someone loads the app just sent out a broadcast to initiate the alarm, again?

  2. What could be causing all the hold up during the HTTPClient request that it locks up my program and forces a ANR? How could I properly implement my HTTPClient request so it wouldn't hold up my Service?

  3. Any good examples of a login/cookie store method. And a service that later on uses a session id to make future requests?

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

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

发布评论

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

评论(1

攒一口袋星星 2025-01-15 04:15:06

当我发出 HTTPClient 请求时,我希望它受到登录保护,所以我想做的是每当 BootService 运行时,它都会向我的网站发起 HTTPClient 请求,登录并存储我将使用的会话 ID在更新服务中。

除非 Web 服务允许无限的会话持续时间,否则这项工作可能会很棘手。我建议您每次更新时都登录。

但是,当我将 HTTPClient 请求放入 BootService 时,我收到 ANR(应用程序未响应)。

这意味着您尝试在 onStartCommand()onCreate()onDestroy() 或<代码>onBind()。所有这些方法都是在主应用程序线程上调用的,因此您无法在那里做重要的工作。

如果我取消 BootService 而只让 MyReceiver 设置 AlarmService 会更好吗?

是的。

什么可能导致 HTTPClient 请求期间的所有阻塞,从而锁定我的程序并强制 ANR?

网络 I/O 可能会很慢。

我怎样才能正确实现我的 HTTPClient 请求,这样它就不会阻碍我的服务?

使用 IntentService 并在 onHandleIntent() 中执行网络 I/O。

When I make my HTTPClient requests I want it to be login protected so what I was trying to do is whenever BootService is run, it will initiate a HTTPClient request to my website, login and store a session id that I'll use in UpdateService.

Unless the Web service allows infinite session durations, making this work could be tricky. I suggest that you do the login on each update.

However, when I put the HTTPClient request into BootService I get a ANR (Application Not Responding).

This means that you were trying to do way too much work in onStartCommand(), or onCreate(), or onDestroy(), or onBind(). All of those methods are called on the main application thread, and so you cannot do significant work there.

Would it be better if I did away with the BootService and instead just have MyReceiver set an AlarmService.

Yes.

What could be causing all the hold up during the HTTPClient request that it locks up my program and forces a ANR?

Network I/O can be slow.

How could I properly implement my HTTPClient request so it wouldn't hold up my Service?

Use an IntentService and do your network I/O in onHandleIntent().

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