package com.ada56.apps.taxi.ui.login;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.ada56.apps.taxi.passenger.R;
public class JSAndroidActivity extends Activity {
private WebView myWebView = null;
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_jsdemo);
myWebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = myWebView.getSettings();// Get the object that sets the properties
webSettings.setJavaScriptEnabled(true);// 使能JavaScript
webSettings.setDefaultTextEncodingName("GBK");//Support Chinese, otherwise Chinese will display garbled characters in the page
// Restrict opening web pages in WebView instead of default browser
myWebView.setWebViewClient (new WebViewClient ());
// If this is not set, the button in the JS code will be displayed, but the dialog box will not pop up when pressed
// Sets the chrome handler. This is an implementation of WebChromeClient
// for use in handling JavaScript dialogs, favicons, titles, and the
// progress. This will replace the current handler.
myWebView.setWebChromeClient (new WebChromeClient () {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
// Call the Android function from JavaScript:
// First build a bridge class, and write the Android code to be called into the public function of the bridge class
// Bind the bridge class to the JavaScript code running in the WebView
// Pass an object as an alias, use this alias instead of this object in JS code, you can call some methods of this object
//myWebView.addJavascriptInterface(new WebAppInterface(this), "myInterfaceName");
myWebView.addJavascriptInterface(new WebViewNative(this), "Native");
//myWebView.loadUrl("file:///android_asset/sample.html");// Load page: local html resource file
myWebView.loadUrl("http://192.168.211.61:8080/taxi/jsAndroid.html");
}
public void callJSMethod(View view){
//Call JavaScript function with Android code:
myWebView.loadUrl("javascript:myFunction('<<callJSMethod>>')");
//The effect achieved here is the same as the effect of clicking the first button in the web page
}
/**
* Bridge class between custom Android code and JavaScript code
*/
public class WebViewNative {
Context mContext;
/** Instantiate the interface and set the context */
WebViewNative(Context c) {
mContext = c;
}
/** Show a toast from the web page */
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_LONG).show();
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="40dp"
android:text="@string/hello_world"
tools:context=".WebJSActivity" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView1"
android:onClick="callJSMethod"
android:text="android_Button" />
<WebView
android:id="@+id/myWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/textView1" />
</RelativeLayout>
===========================================================================================================================
<html>
<head>
<meta charset="UTF-8">
<!-- JavaScript script, mainly including the function to be executed by the button, displaying the dialog box, etc.-->
<script type="text/javascript">
//JavaScript method, pop-up dialog box to display information
function myFunction(msg){
alert("myFunction is called, msg="+msg);
}
function onAlert()
{
console.log("onAlert method");//Display debugging information
alert("This is a alert sample from html");
}
function onConfirm()
{
console.log("onConfirm method");
var b = confirm("are you sure to login?");
alert("your choice is " + b);
}
function onPrompt()
{
console.log("onPrompt method");
var b = prompt("please input your password", "aaa");
alert("your input is " + b);
}
//Call the method of the bound Java object, that is, call the Android code to display the dialog box
function showAndroidToast(toast)
{
console.log("showAndroidToast method");
//myInterfaceName.showToast(toast);//Note that the myInterfaceName here should be the same as the name passed in from the outside, and the case is correct
Native.showToast(toast);
}
</script>
</head>
<body>
<p>
<!-- The first four buttons call JS functions -->
JavaScript function call<br />
<button onclick="myFunction()">Click here!</button>
<br />
<input type="button" value="alert" onclick="onAlert()" /> <br />
<input type="button" value="confirm" onclick="onConfirm()" /> <br />
<input type="button" value="prompt" onclick="onPrompt()" /><br />
<!-- There are two ways to define buttons above, the effect is the same -->
</p>
<p>
<!-- This Say hello button calls a method in Android code -->
Calling Android code with JavaScript buttons<br />
<input type="button"
value="Say hello" onClick="showAndroidToast('Hello Android!')" />
</p>
<a href="http://www.google.com" />Google
</a>
</body>
</html>