Android webview 里面有一个服务吗?

发布于 2024-12-11 20:54:48 字数 251 浏览 2 评论 0原文

我的 Android 应用程序中有一个 WebView,我们的服务器从中调用了很多 javascript。我想实现一个系统,其中来自服务器的信号将唤醒/重新启动应用程序并将其带到前面以提醒用户。我知道这非常令人讨厌,但就我而言这是必要的。

似乎实现这一点的唯一方法是让 WebView 存在于 Service 中(因为 Activity 可以随时被操作系统终止)。有谁知道这是如何运作的?我读到 WebView 只能驻留在 Activity 中。

谢谢!!

I have a WebView in my android app from which a lot of javascript is getting called by our server. I want to implement a system where a signal from the server will wake up/restart the app and bring it to the front to alert the user. I know this is horribly obnoxious but in my case its necessary.

It seems that the only way to accomplish this is to have the WebView live in a Service (because Activities can be killed by the OS at any time). Does anyone know how this would work? I've read that WebViews can only reside in Activities.

Thanks!!

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

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

发布评论

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

评论(2

明媚如初 2024-12-18 20:54:48

WebView 只能驻留在 Activity 中,因为它们是 UI 组件。解决此问题的最佳方法是将 javascript 正在进行的通信卸载到服务本身中。这就是 Android 的工作原理。唯一的其他解决方案是使您的 javascript/服务器通信具有足够的容错能力,使其能够承受随时失去连接的情况。对于如何做到这一点的想法,我会研究 REST 模型。如果您想更好地了解如何在 Android 中使用 REST,我建议您观看来自 Google IO 的 视频。直到今天,它仍然是我的最爱之一。

就从服务器发送信号来启动活动而言,这在某种程度上是可能的。您需要在设备上监听一些东西,很可能是一项服务。您可以让您的服务进行长时间轮询,然后坐在那里等待响应。当它得到服务器的响应时,它可以执行一个意图,

Context.startActivity(intent)

其中意图是指定您要启动的活动的显式意图。也就是说,如果设备上的资源变得稀缺,服务也可能被终止。最重要的是,互联网连接可能随时丢失(这些是移动电话,人们经常将移动电话带到没有互联网连接的地方)。

最重要的是,这些设备在连接方面并不是 100% 可靠的。对您来说,最明智的做法是更改应用程序规范,以便它们具有更高的容错能力。

这听起来可能很直率,但除了“不要这样做。永远不要这样做”之外,我无话可说。更具建设性的是,我想说,如果你做得正确,你应该能够将你的东西推到后台,并且永远不会打扰用户。请记住,KISS,这不仅仅是一支拥有军队的乐队。这是一个美丽的哲学。保持简单愚蠢。您可能想看一下这个视频,它是我最喜欢的视频之一,可能会帮助您进入不同的状态头脑。有帮助吗?

WebViews can only ever reside in activities because they are a UI component. The best way for you to solve this problem is to offload the communication that your javascript is doing into the service itself. This is just how android works. The only other solution is to make your javascript/server communication fault-tolerant enough that it can withstand loosing connection at any time. For ideas on how to do this, I'd look into the REST model. If you'd like to get a better idea on using REST with android, I suggest this video from Google IO. To this day, it's one of my favorites.

As far as sending a signal from the server to start up an activity, this is a possibility to some extent. You'll need to have something listening on the device, most likely a service. You could have your service do a long-poll and therefor just sitting there waiting for a response. When it get's the response from the server, it could then do a

Context.startActivity(intent)

Where intent is an explicit intent specifying the activity you want to start. That said, a service can be killed also if resources are getting to scarce on the device. On top of that, internet connectivity could be lost at any time (these are mobile phones and people often take mobile phones places where they don't have internet connectivity).

The bottom line is, the devices aren't designed to be 100% reliable in terms of connectivity. The smartest thing for you to do would be to change you app specs so that they're more fault tolerant.

It may sound blunt, but there's not much more that I can say other than "Don't do this. Don't do this ever." More constructively, I'd say that if you do things right, you should be able to push your stuff into the background and never bother the user about with it. Remember, KISS, it's not just a band with an army. It's a beautiful philosophy. Keep it simple stupid. You might wanna take a look at this video, it's one of my favorite and might help get you in a different state of mind. Is that helpful?

上课铃就是安魂曲 2024-12-18 20:54:48

TomBomb,感谢您接受我之前的回答,但我想我可能还有其他对您有用的东西。为什么不使用 状态栏,而不是强制 Activity 进入前台通知让您的用户知道服务器上有新内容?

TomBomb, thanks for accepting my previous answer but I think I might have something else of use for you. Instead of forcing an Activity to come into the foreground, why not use a Status Bar Notification to let your users know something new from the server has come in?

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