里面有服务的Andr​​oid的WebView? [英] Android webview inside a service?

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

问题描述

我在我的Andr​​oid应用程序一个的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.

似乎完成此的唯一方法是具有web视图生活在一个服务(因为活动可以在任何时候被杀死由OS)。有谁知道这是如何工作的?我读过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,我建议从谷歌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.

这可能听起来生硬,但有没有更多的,我可以说比其他的不要这样,不要这样做吧。更积极,我会说,如果你做正确的事情,你应该能够你的东西推到背景,从不打扰用户有关它。请记住,亲吻,它不只是一个乐队与军队。这是一个美丽的理念。保持简单愚蠢。你可能会想看看这个影片,这是我最喜欢的之一,并可能帮助你在不同的状态心神。那是有益的?

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?

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

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