draco1023
10/18/2016 - 9:27 AM

Baidu map in Android WebView

Baidu map in Android WebView

package com.example.hello_map;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

        private WebView mWebView;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                mWebView = (WebView) this.findViewById(R.id.webview1);
                WebSettings webSettings = mWebView.getSettings();
                
                webSettings.setAllowFileAccess(true);//设置启用或禁止访问文件数据
                webSettings.setDomStorageEnabled(true); // Display
                webSettings.setJavaScriptEnabled(true); //设置是否支持JavaScript        
                webSettings.setBlockNetworkImage(false);        
                webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);
//        webSettings.setPluginsEnabled(true);
//                webSettings.setSupportZoom(false);//设置是否支持变焦
                MyWebViewClient myWebViewClient = new MyWebViewClient();
                mWebView.setWebViewClient(myWebViewClient);
                mWebView.loadUrl("file:///android_asset/test.html");
        }
    /*
     * Class  :  MyWebViewClient,用于辅助WebView,处理各种通知、请求等事件
     * Author  :    博客园-依旧淡然
     */
    private class MyWebViewClient extends WebViewClient {
        //重写父类方法,让新打开的网页在当前的WebView中显示
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            System.out.println("---------------------------------------------------------");
            return true;
        }//扩充缓存的容量  
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){   
                System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq");
                System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq");
            //handler.cancel(); 默认的处理方式,WebView变成空白页     
                //          //接受证书     
                handler.proceed();  
           //handleMessage(Message msg); 其他处理     
        } 
        
    }
}