最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Android webview inside a service? - Stack Overflow

programmeradmin0浏览0评论

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 acplish 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!!

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 acplish 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!!

Share Improve this question asked Oct 25, 2011 at 4:04 TomBombTomBomb 3,2965 gold badges33 silver badges41 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

WebViews can only ever reside in activities because they are a UI ponent. The best way for you to solve this problem is to offload the munication 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 munication 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?

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 e into the foreground, why not use a Status Bar Notification to let your users know something new from the server has e in?

发布评论

评论列表(0)

  1. 暂无评论