Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

javascript - Android webview inside a service?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...