简体   繁体   English

Webview加载远程URL

[英]Webview load remote url

First, a disclaimer: i'm completely new to iphone, android or any mobile development. 首先,免责声明:我对iPhone,Android或任何移动开发完全陌生。 In the other side, i've been developing websites (php, javascript) for long time. 另一方面,我一直在开发网站(php,javascript)。

I have a website which is adapted for mobile and works great. 我有一个适合移动设备的网站,效果很好。 But, there are some features i need such as prevent the screen from dimming (my site is designed to be used for many hours without user interaction while being permanently visible or for example being able to send notifications (with sound, etc., to attract user attention)). 但是,我需要一些功能,例如防止屏幕变暗(我的网站被设计为可以长时间使用,而无需用户交互,而它们始终可见,或者能够发送通知(带有声音等)以吸引用户用户注意))。

This has lead me to consider a webview. 这使我考虑了网络视图。 From what i've seen, it's about creating a native app which just a webview (browser without toolbars) and using html and javascript to operate. 从我所看到的,这是关于创建一个本地应用程序,该应用程序只是一个webview(没有工具栏的浏览器),并使用html和javascript进行操作。 It will use some native functions to perform some native actions (such as the ones i want). 它将使用一些本机函数来执行某些本机操作(例如我想要的操作)。

I've searched around and i don't have a specific response to this: can i tell a webview to, permanently load the content from a remote site ? 我已经四处搜寻,但对此没有具体回应:我可以告诉Webview永久从远程站点加载内容吗? i mean, my site is php based (zend), with many jquery content manipulation. 我的意思是,我的网站基于php(zend),具有许多jquery内容操作。 Can i tell him something like LOAD htp://www.mysyite.com and let him do everything else from it ? 我可以告诉他类似LOAD htp://www.mysyite.com之类的东西,然后让他做其他事情吗? absolutely no local content applies, everything is remote. 绝对没有本地内容适用,一切都是远程的。 The webview would just be an "interface" to the website. Webview只是网站的“界面”。

And additional question is: can i use jquery on it ? 另外一个问题是:我可以在上面使用jquery吗? ajax calls ? 阿贾克斯电话? geolocation ? 地理位置? i mean, in a browser i can, i just wonder if inside a webview i can. 我的意思是,在浏览器中我可以,我只是想知道是否可以在Webview中。

I've read that phonegap does this. 我读过phonegap可以做到这一点。 But most of the time, when taking about phonegap and about webview i general, i read people talking about loading locally the page not remotely in a permanent basis. 但是大多数时候,当我谈到电话间隙和一般的Webview时,我读到的人都在谈论本地永久性地加载页面。

Finally, yes, i will build a native app in the future. 最后,是的,我将来会构建一个本机应用程序。 But now, i simply don't have time to learn about android, ios and blackberry at the same time. 但是现在,我根本没有时间同时学习android,ios和blackberry。 Thanks a lot for your responses. 非常感谢您的回复。

For Android only: 仅适用于Android:

Can i tell him something like LOAD htp://www.mysyite.com and let him do everything else from it ? absolutely no local content applies, everything is remote. The webview would just be an "interface" to the website.

Yes, of course you can. 是的,当然可以。 Suppose you have a webview in an activity (it's really easy), you would load the web page something like: 假设您在一个活动中有一个Web视图(这确实很简单),那么您将加载以下网页:

WebView myWebView = .... // get a reference from XML or if you just created get its ref
myWebView.loadURL("http://www.mysite.com");

I would start with this API Guide article. 我将从这篇API指南文章开始。 Also, taken from WebView JavaDoc : 另外, 取自WebView JavaDoc

A WebView has several customization points where you can add your own behavior. WebView具有多个自定义点,您可以在其中添加自己的行为。 These are: 这些是:

  1. Creating and setting a WebChromeClient subclass. 创建和设置WebChromeClient子类。 This class is called when something that might impact a browser UI happens, for instance, progress updates and JavaScript alerts are sent here (see Debugging Tasks) 当发生可能影响浏览器用户界面的事情时,例如,在此处发送进度更新和JavaScript警报时,将调用该类(请参阅调试任务)
  2. Creating and setting a WebViewClient subclass. 创建和设置WebViewClient子类。 It will be called when things happen that impact the rendering of the content, eg, errors or form submissions. 当发生影响内容呈现的事情(例如错误或表单提交)时,将调用该方法。 You can also intercept URL loading here (via shouldOverrideUrlLoading()). 您也可以在此处拦截URL加载(通过shouldOverrideUrlLoading())。
  3. Modifying the WebSettings , such as enabling JavaScript with setJavaScriptEnabled(). 修改WebSettings ,例如使用setJavaScriptEnabled()启用JavaScript。 Injecting Java objects into the WebView using the addJavascriptInterface(Object, String) method. 使用addJavascriptInterface(Object,String)方法将Java对象注入WebView。 This method allows you to inject Java objects into a page's JavaScript context, so that they can be accessed by JavaScript in the page. 此方法使您可以将Java对象注入页面的JavaScript上下文中,以便页面中的JavaScript可以访问它们。

Please be also aware that the webview is not that powerful as the phone's browser. 另请注意,Web视图不如手机的浏览器强大。 Here is a SO thread where a friend posted an interesting question. 这是一个SO线程 ,朋友在其中发布了一个有趣的问题。 You might find helpful the answers he got. 您可能会发现他得到的答案很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM