服务内的Android webview? [英] Android webview inside a service?

查看:15
本文介绍了服务内的Android webview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

似乎实现此目的的唯一方法是将 WebView 存在于服务中(因为操作系统可以随时终止活动).有谁知道这将如何工作?我已经读过 WebViews 只能驻留在活动中.

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.

谢谢!!

推荐答案

WebViews 只能驻留在活动中,因为它们是一个 UI 组件.解决此问题的最佳方法是将 javascript 正在进行的通信卸载到服务本身中.这就是安卓的工作原理.唯一的其他解决方案是使您的 javascript/服务器通信具有足够的容错能力,以便它可以随时承受断开连接.有关如何执行此操作的想法,我会研究 REST 模型.如果您想更好地了解如何在 android 上使用 REST,我建议您观看 Google IO 视频.直到今天,它还是我的最爱之一.

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).

最重要的是,这些设备在连接方面并非设计为 100% 可靠.对您来说,最明智的做法是更改应用规范,使其具有更高的容错性.

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.

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

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?

这篇关于服务内的Android webview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆