1. Build objects to interact with

public static class TestObject {
    private final WeakReference<Activity> wfActivity;
    public TestObject(Activity ac) {
        this.wfActivity = new WeakReference<>(ac);
    // Handle the HTML button trigger event
    public void login(String uid) {
        if(wfActivity.get() ! =null) {}}@JavascriptInterface
    public void loginOut(a) {}}Copy the code

2. WebViewSet up theWebViewClient

mWebView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return super.shouldOverrideUrlLoading(view, request);
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return super.shouldOverrideUrlLoading(view, url);
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    public void onPageFinished(WebView view, String url) {
    	// Pass the data to HTML
        final UserInfo bean = DataSourceManager.getUserInfoBen();
        if(bean ! =null) {
            final String token = ACache.get(context).getAsString(Constants.TOKEN);
            mWebView.evaluateJavascript("javascript:getUserInfo('" + bean.user_id + "', '" + bean.avatar + "', '" + token + "')".new ValueC
                public void onReceiveValue(String value) {
                    //Log.e("123", "onReceiveValue getUserInfo : " + value);}}); }super.onPageFinished(view, url);
    public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
        super.doUpdateVisitedHistory(view, url, isReload); }}); mWebView.addJavascriptInterface(new TestObject(mMainActivity), "testObject");
Copy the code


<html lang="zh-cn">
	// H5 receives user information from the mobile terminal
        function getUserInfo(uid,pwd,avatar){
           return "User Information :"+ uid+""+pwd +""+avatar;
	// H5 receives the mobile terminal exit action
        function exitUser(uid){
           return "Exit account:"+ uid;
<body bgcolor="#BCBCBC">
        <button type="button" onclick="testObject.login(uid);">H5 Sends the user information to the mobile terminal. The mobile terminal logs in using the corresponding login method</button>
        <button type="button" onclick="testObject.loginOut(uid);">HTML user exit operation, mobile processing loginOut method</button>
Copy the code