Android builds Web applications on WebView, androidwebview
Link: http://developer.android.com/guide/webapps/webview.html
Reference: http://developer.android.com/reference/android/webkit/WebView.html
If you want to implement a Web application (or just a Web page) as part of your application, you can use WebView to implement it. WebView is an extension of the Android View class. It allows you to display a webpage as part of the Activity layout. It does not contain mature browser functions, such as navigation control or input bar. By default, WebView displays a webpage.
A common use of WebView is that when you want to provide information that needs to be updated frequently in applications, such as user protocols or user manuals. In the Android app, you can create an Activity that contains a WebView, and then use it to display documents provided online.
Another use case of WebView is: if your application always needs a network link to obtain data and provide it to users, such as e-mails. At this time, you can create a WebView in the application and use the webpage to display user data, which is more convenient than executing network requests, parsing data, and then rendering the data to the Android layout. You only need to design a webpage based on the Android device, implement a WebView in the app, and load the webpage you just designed.
This document will teach you how to learn about WebView and implement some other functions, such as page navigation and binding JavaScript to webpages.
To add a WebView to an application, you only need to include the WebView tag in your Activity layout file. For example, the following is a WebView filled with full screen layout.
<?xml version="1.0" encoding="utf-8"?><WebView xmlns:android=http://schemas.android.com/apk/res/android android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
To load a webpage in WebView, you can use the loadUrl () method, for example
WebView myWebView = (WebView) findViewById(R.id.webview);myWebView.loadUrl("http://www.baidu.com");
Before running this code, you need to access the Internet. You only need to add Internet permissions to your Manifest file.
<uses-permission android:name="android.permission.INTERNET" />
These are the basic knowledge of displaying a Web page.
Use JavaScript in WebView. If the webpage you want to load implements JavaScript, you must enable the JavaScript function in WebView. When JavaScript is available, you can create interfaces between your application code and JavaScript code.
By default, JavaScript is disabled when JavaScript is enabled in WebView. You can enable WebView by adding WebSettings. You can use the getSettings () method to obtain WebSettings, and then use setJavaScriptEnabled () to enable it. For example:
WebView myWebView = (WebView) findViewById(R.id.webview);WebSettings webSettings = myWebView.getSettings();webSettings.setJavaScriptEnabled(true);
You can find many useful functions in WebSettings. For example, if you use WebView to develop a network application, you can use the setUserAgentString () method to define a Custom User proxy string, and then you can query the Custom User proxy in the web page, identify whether the requested webpage is your Android app.
Binds JavaScript code to the Android source code. When WebView is used in an application to create a network application, you can create an interface between JavaScript and Android Source Code. For example, your JavaScript code can call the method defined in Android to display a dialog box, rather than the JavaScript alert () method.
For example, you can add the following classes to an application:
public class JavaScriptInterface {Context mContext;/** Instantiate the interface and set the context */JavaScriptInterface(Context c) { mContext = c; }/** Show a toast from the web page */ public void showToast(String toast) { Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); }}
In this example, the JavaScriptInterface class uses the showToast () method to create a Toast message.
You can bind this class to JavaScript and run the addJavaScriptInterface () method in WebView and create the interface named Android.
WebView webView = (WebView) findViewById(R.id.webview);webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
This Code creates a JavaScript interface named "Android" and runs it in WebView. Here, your Web application is connected to the JavaScriptInterface class. For example, the following is the Html and JavaScript code. It creates a Toast message using a new interface when you click the button.
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /><script type="text/javascript"> function showAndroidToast(toast) { Android.showToast(toast); }</script>
You do not need to initialize the Android interface from JavaScript. WebView automatically allows the WebView to be used on the webpage. Therefore, when you click the button, the showAndroidToast () method uses the Android interface to call the JavaScriptInterface. showToast () method.
Remarks: The object bound to JavaScript will run in another thread, which is different from the thread used to create it.
Notes: Java. lang. String) addJavaScriptInterface () allows JavaScript to control Android applications. This is very useful and dangerous. When HTML in WebView is untrusted (for example, some content in HTML is provided by unknown people or threads), attackers can freely control the HTML code executed on the client. Similarly, except for HTML or JavaScript that you need to write in WebView, you should not use java. lang. String) addJavaScriptInterface () method. You cannot allow users to manipulate webpages other than yourself in WebView (Instead, you should have users call the default browser to open external connections-general, the user's web browser opens all URL links, so only when the webpage to be manipulated is the same as that described in the following section ).
Processing page navigation when a user clicks a link in your WebView, an app is started to capture the default action for Android. Generally, the default web browser opens and loads the target URL. However, you can rewrite the action in WebView to open the link in your WebView. You can also allow users to handle historical rollback and forward Functions on their own.
Open the link clicked by the user and only provide WebViewClient in WebView. Use the setWebViewClient () method. For example:
WebView myWebView = (WebView) findViewById(R.id.webview);myWebView.setWebViewClient(new WebViewClient());
Now all the links clicked by the user are opened in WebView. If you want to perform more operations when you click the link and load the webpage, create your own WebViewClient and rewrite the java. lang. String) shouldOverrideUrlLoading () method. For example:
Private class MyWebViewClient extends WebViewClient {@ Override public boolean shouldOverrideUrlLoading (WebView view, String url) {if (Uri. parse (url ). getHost (). equals ("www.example.com") {// This Is My webpage, so do not rewrite it and let my WebView load it return false;} // if it is not my webpage, then start another Activity to process the URLIntent intent = new Intent (Intent. ACTION_VIEW, Uri. parse (url); startActivity (intent); return true ;}}
Create a WebViewClient instance for WebView.
WebView myWebView = (WebView) findViewById(R.id.webview);myWebView.setWebViewClient(new MyWebViewClient());
Now, when you click a link, the system will call java. lang. string) shouldOverrideUrlLoading (), which identifies whether the URL host matches the specified domain name (as listed above ). If it does not match, the method returns false. An Intent creates and starts the default Activity to process the URL (which is generally parsed as the user's default browser ).
Operation page history when your WebView overwrites the loading of a URL, it automatically accumulates the accessed page history. You can use the goBack () and goForward () Methods to manipulate the rollback and forward functions. For example, the following shows how the Activity uses the return button to perform the "rollback" function.
@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// Check if the key event was the Back button and if there's historyif ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack() { myWebView.goBack(); return true; } // If it wasn't the Back key or there's no web page history, bubble up to the default // system behavior (probably exit the activity) return super.onKeyDown(keyCode, event);}
If there is a Web page history, the canGoBack () method returns True. Similarly, you can use canGoForward () to check whether there is a forward history. If you do not perform this check, goBack () and goForward () will not do anything when the user reaches the end of history.