Solve Chinese garbled characters:
1. Use getBytes ("encoding method"); to re-encode Chinese characters and obtain their byte arrays.
2. Use new String (Bytes [], "decoding method") to decode the byte array.
WebKit Application
WebKit consists of three modules: javascriptCore, webCore, and webKit.
WebView web browsing:
Important:
WebViewClient is a class dedicated to assisting WebView in handling various notifications, requests, and other events.
WebChromeClient is a dialog box that assists WebView in processing javascript, website icons, website titles, and loading progress.
Package com.king.android.net;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. AlertDialog. Builder;
Import android. content. Context;
Import android. content. DialogInterface;
Import android. graphics. Bitmap;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. Window;
Import android. view. View. OnClickListener;
Import android. webkit. JsPromptResult;
Import android. webkit. JsResult;
Import android. webkit. URLUtil;
Import android. webkit. WebChromeClient;
Import android. webkit. WebSettings;
Import android. webkit. WebView;
Import android. webkit. WebViewClient;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TextView;
Import com. king. android. R;
/**
* Description: Webpage Browsing.
* Author: Andy. Liu
* Time: 07:28:17
**/
Public class WebViewActivity extends Activity {
Context mContext;
Button btnConn;
EditText etUrl;
WebView wvShow;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
MContext = WebViewActivity. this;
SetContentView (R. layout. webview_layout );
BtnConn = (Button) findViewById (R. id. btn_conn );
EtUrl = (EditText) findViewById (R. id. ed_show );
WvShow = (WebView) findViewById (R. id. wv_view );
BtnConn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Try {
String url = etUrl. getText (). toString ();
If (URLUtil. isNetworkUrl (url )){
WvShow. loadUrl (url );
} Else {
EtUrl. setText ("Enter URL error, please enter again ");
} Catch (Exception e ){
}
}});
The following are the main knowledge points:
// Set the javascript script
WebSettings webSettings = wvShow. getSettings ();
WebSettings. setJavaScriptEnabled (true );
// Set File Access
WebSettings. setAllowFileAccess (true );
// Supports scaling.
WebSettings. setBuiltInZoomControls (true );
/**
* Set WebViewClient
* WebViewClient is a class dedicated to assisting WebView in handling various notifications, requests, and other events.
**/
WvShow. setWebViewClient (webViewClient );
/**
* WebChromeClient is a dialog box that assists WebView in processing javascript, website icons, website titles, and loading progress.
**/
WvShow. setWebChromeClient (webChromeClient );
}
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (KeyEvent. KEYCODE_BACK = keyCode ){
// Return to the previous page
WvShow. goBack ();
Return true;
}
Return super. onKeyDown (keyCode, event );
}
Private WebViewClient webViewClient = new WebViewClient (){
Public boolean shouldOverrideUrlLoading (WebView view, String url ){
View. loadUrl (url );
Return true;
}
Public void onPageFinished (WebView view, String url ){
Super. onPageFinished (view, url );
}
Public void onPageStarted (WebView view, String url, Bitmap favicon ){
Super. onPageStarted (view, url, favicon );
}
};
Private WebChromeClient webChromeClient = new WebChromeClient (){
// Process alert in js
Public boolean onJsAlert (WebView view, String url, String message, final JsResult result ){
Builder builder = new Builder (mContext );
Builder. setTitle ("prompt dialog box ");
Builder. setMessage (message );
Builder. setPositiveButton (android. R. string. OK, new AlertDialog. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// Click OK to continue the operation on the webpage
Result. confirm ();
}});
Builder. setCancelable (false );
Builder. create ();
Builder. show ();
Return true;
};
Public boolean onJsConfirm (WebView view, String url, String message, final android. webkit. JsResult result ){
Builder builder = new Builder (mContext );
Builder. setTitle ("selected dialog box ");
Builder. setMessage (message );
Builder. setPositiveButton (android. R. string. OK, new AlertDialog. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// Click OK to continue the operation on the webpage
Result. confirm ();
}});
Builder. setNegativeButton (android. R. string. cancel, new AlertDialog. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// Click Cancel
Result. cancel ();
}});
Builder. setCancelable (false );
Builder. create ();
Builder. show ();
Return true;
};
@ Override
// Process the prompt in javascript
// Message indicates the prompt content in the dialog box on the webpage.
// Content displayed by default when defavalue value is not input
Public boolean onJsPrompt (WebView view, String url, String message, String defaultValue, final JsPromptResult result ){
// A custom dialog box with input is composed of TextView and EditText.
Final LayoutInflater factory = LayoutInflater. from (mContext );
Final View dialogview = factory. inflate (R. layout. prom_dialog, null );
(TextView) dialogview. findViewById (R. id. TextView_PROM). setText (message );
(EditText) dialogview. findViewById (R. id. EditText_PROM). setText (defaultValue );
Builder builder = new Builder (mContext );
Builder. setTitle ("dialog box with input ");
Builder. setView (dialogview );
Builder. setPositiveButton (android. R. string. OK,
New AlertDialog. OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){
// Click OK to obtain the input value and send it to the webpage for processing.
String value = (EditText) dialogview. findViewById (R. id. EditText_PROM). getText (). toString ();
Result. confirm (value );
}
});
Builder. setNegativeButton (android. R. string. cancel,
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){
Result. cancel ();
}
});
Builder. setOnCancelListener (new DialogInterface. OnCancelListener (){
Public void onCancel (DialogInterface dialog ){
Result. cancel ();
}
});
Builder. show ();
Return true;
};
@ Override
// Set the webpage loading progress bar
Public void onProgressChanged (WebView view, int newProgress)
{
WebViewActivity. this. getWindow (). setFeatureInt (Window. FEATURE_PROGRESS, newProgress * 100 );
Super. onProgressChanged (view, newProgress );
}
@ Override
// Set the title of the application
Public void onReceivedTitle (WebView view, String title)
{
WebViewActivity. this. setTitle (title );
Super. onReceivedTitle (view, title );
}
};
}
Pay attention to the following aspects of webView: