Mutual calls between Android and the Web

Source: Internet
Author: User
<span id="Label3"></p><p><p>This article simply explains the simple interaction between Android and the Web.<br>1:android displaying Web pages: using Android's webview controls to show Web pages<br>2:web Web page calls the Android method: use annotations to expose the methods of android, and to invoke public methods in Javascript.</p></p><p><p>next, Show specific Small examples:</p></p><p><p><strong>1:webview Display Web Pages:</strong></p></p><p><p>Layout:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs handlebars"><span class="xml"><span class="hljs-tag"><<span class="hljs-title">WebView</span> <span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/loadWeb"</span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span> <span class="hljs-attribute">android:layout_below</span>=<span class="hljs-value">"@+id/web"</span></<span class="hljs-attribute">WebView</span>></span></span></code></pre></pre><p><p>Code:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"> webView = (WebView)findViewById(R.id.loadWeb); <span class="hljs-comment">//获得对webview的设置</span> WebSettings settings = webView.getSettings(); <span class="hljs-comment">//设置允许缩放</span> settings.setSupportZoom(<span class="hljs-keyword">true</span>); <span class="hljs-comment">//设置允许脚本</span> settings.setJavaScriptEnabled(<span class="hljs-keyword">true</span>); <span class="hljs-comment">//设置客户端</span> webView.setWebChromeClient(<span class="hljs-keyword">new</span> WebChromeClient()); webView.setWebViewClient(<span class="hljs-keyword">new</span> WebViewClient()); <span class="hljs-comment">//加载网址</span> webView.loadUrl(<span class="hljs-string">"http://www.baidu.com"</span>);</code></pre></pre><p><p>Last Note Configure Permissions:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.INTERNET"</span> /></span></code></pre></pre><p><p>Run will be able to see Baidu home Page.</p></p><p><p><strong>2:web Call the Android method:</strong></p></p><p><p>1: layout:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs handlebars"><span class="xml"><span class="hljs-tag"><<span class="hljs-title">WebView</span> <span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/loadWeb"</span> <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span> <span class="hljs-attribute">android:layout_below</span>=<span class="hljs-value">"@+id/web"</span></<span class="hljs-attribute">WebView</span>></span></span></code></pre></pre><p><p>2:android Public Methods:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">class</span> LoadJS { <span class="hljs-keyword">private</span> Context context; <span class="hljs-keyword">public</span><span class="hljs-title">LoadJS</span>(Context context){ <span class="hljs-keyword">this</span>.context = context; } <span class="hljs-comment">//以注解的方式公开该方法(webkit是浏览器内核),以供网页调用</span> @android.webkit.JavascriptInterface <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">MyToast</span>(String cc){ <span class="hljs-string">"我来自网页调用"</span>+cc, Toast.LENGTH_SHORT).show(); }}</code></pre></pre><p><p>3: Web page Call public method:<br>Create a new Assets folder under the Android project folder Src–>main and create a new HTML page in the assets Folder.<br>HTML code, where the JS part of the code can be completed after the completion of the annotation public method of Android:</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">html</span>></span></span><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">head</span>></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">title</span>></span></span>Page title<span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">title</span>></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">script</span>></span></span><span class="javascript"><span class="javascript"> <span class="hljs-function"> <span class="hljs-keyword">function</span> <span class="hljs-title">clickq</span><span class="hljs-params">()</span>{</span> <!--get the value of the input box-- <span class="hljs-keyword">var</span> cc = Docume Nt.getelementbyid (<span class="hljs-string">"content"</span>). value; <span class="xml"> <span class="hljs-comment"><!--call public methods-</span> -android. Mytoast (cc); }</span> </span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">script</span>></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">head</span>></span></span><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">body</span>></span></span><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"click input"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"content"</span>/></span></span><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"button"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"pushbutton</span> " <span class="hljs-attribute">onclick</span> =<span class="hljs-value">"CLICKQ ()"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"button"</span>/></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">body</span>></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">html</span>></span></span></code></pre><p><p>Can be Run.</p></p> <p><p>Mutual calls between Android and the Web</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.