Ejemplo n.º 1
0
  @SuppressLint({"NewApi", "SetJavaScriptEnabled"})
  private void loadWebView() {
    // 实例化WebView对象
    // webview = new MyWebView(WebViewActivity.this);
    LogUtil.i(TAG, "loadWebView===实例化WebView===");
    webview = (MyWebView) findViewById(R.id.id_webview);
    new MobclickAgentJSInterface(this, webview);

    WebSettings webSettings = webview.getSettings();
    // 设置WebView属性,能够执行Javascript脚本
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    //
    webSettings.setUseWideViewPort(true); // 关键点
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setAppCacheEnabled(true);

    webSettings.setAppCacheMaxSize(8 * 1024 * 1024); // 8MB
    // webSettings.setAppCachePath(Constants.WEBVIEW_CACHE_DIR );
    String appCacheDir =
        this.getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath();
    webSettings.setAppCachePath(appCacheDir);
    webSettings.setAllowFileAccess(true);
    webSettings.setDomStorageEnabled(true);

    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

    // js调用安卓方法
    webview.addJavascriptInterface(this, "RedirectListner");
  }
Ejemplo n.º 2
0
 /** JS调用的方法 */
 @JavascriptInterface
 public void reloadUrl(String url) {
   Log.i(TAG, "reloadUrl()");
   HashMap<String, String> hashmap = new HashMap<String, String>();
   if (null != SharedPrefsUtil.getValue(Constants.TOKEN, null)) {
     hashmap.put("token", SharedPrefsUtil.getValue(Constants.TOKEN, null));
   }
   webview.clearView();
   webview.loadUrl(url, hashmap);
 }
Ejemplo n.º 3
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    myProgressDialog2 = new MyProgressDialog(this);
    myProgressDialog2.setMessage("正在请求...");
    myProgressDialog2.show();

    context = this;
    intent = getIntent();
    loadUrl = intent.getStringExtra("loadUrl");

    bt_update = (Button) findViewById(R.id.id_bt_update);
    // commonActivityTopView = (CommonActivityTopView)
    // findViewById(R.id.id_CommonActivityTopView);
    // commonActivityTopView.tv_title.setText("webview");
    rl = (RelativeLayout) findViewById(R.id.id_rl);

    loadWebView();
    webViewListener();
    initListener();
    LogUtil.i(TAG, SharedPrefsUtil.getValue(Constants.TOKEN, null));
    HashMap<String, String> hashmap = new HashMap<String, String>();
    if (null != SharedPrefsUtil.getValue(Constants.TOKEN, null)) {
      hashmap.put("token", SharedPrefsUtil.getValue(Constants.TOKEN, null));
    }
    webview.loadUrl(loadUrl, hashmap);
  }
Ejemplo n.º 4
0
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {
      LogUtil.i(TAG, "==onKeyDown==");
      if (isPageLoaded) {
        webview.loadUrl("javascript:CommonRedirect.goBack()");
        LogUtil.i(TAG, "==onKeyDown==isPageLoaded==");
      } else {
        if (webview.canGoBack()) {
          webview.goBack();
        } else {
          finish();
        }
      }
    }
    return false;
  }
Ejemplo n.º 5
0
 /** JS调用的方法 */
 @JavascriptInterface
 public void goToUpdateUrl() {
   Log.i(TAG, "goToUpdateUrl()");
   webview.loadUrl("file:///android_asset/a.html?'" + current_url + "'");
   // finish();
 }
Ejemplo n.º 6
0
  private void webViewListener() {
    webview.setWebViewClient(
        new WebViewClient() {
          // 加载失败
          @Override
          public void onReceivedError(
              WebView view, int errorCode, String description, String failingUrl) {

            LogUtil.i(
                TAG, "加载失败===" + errorCode + "---" + description + "---" + failingUrl + "---");

            current_url = failingUrl;
            webview.clearView();
            if (failingUrl.contains("#")) {
              String[] temp;
              temp = failingUrl.split("#");
              webview.loadUrl(temp[0]);
              try {
                Thread.sleep(400);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              webview.loadUrl(failingUrl);
            } else {
              webview.loadUrl("file:///android_asset/error.html#" + failingUrl);
            }

            ToastUtil.showMessage("页面加载失败,请点击重新加载");

            // super.onReceivedError(view, errorCode, description,
            // failingUrl);

          }

          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
            /*
             * Toast.makeText(getApplicationContext(),
             * "WebViewClient.shouldOverrideUrlLoading",
             * Toast.LENGTH_SHORT);
             */
            LogUtil.i(TAG, "拦截url---shouldOverrideUrlLoading-->" + url);
            if (url.startsWith("tel:")) {
              Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
              startActivity(intent);
            } else {
              HashMap<String, String> hashmap = new HashMap<String, String>();
              if (null != SharedPrefsUtil.getValue(Constants.TOKEN, null)) {
                hashmap.put("token", SharedPrefsUtil.getValue(Constants.TOKEN, null));
              }
              webview.loadUrl(url, hashmap);
            }

            return true;
          }

          @Override
          public void onLoadResource(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onLoadResource(view, url);
          }

          @Override
          public void onPageStarted(WebView view, String url, Bitmap favicon) {
            isPageLoaded = false;
            LogUtil.i(TAG, "拦截url---onPageStarted-->" + url);
            HashMap<String, String> hashmap = new HashMap<String, String>();
            if (null != SharedPrefsUtil.getValue(Constants.TOKEN, null)) {
              hashmap.put("token", SharedPrefsUtil.getValue(Constants.TOKEN, null));
            }
            if (url.toLowerCase().contains("/login")) {
              Intent i11 = new Intent();
              i11.setClass(WebViewActivity.this, LoginActivity.class);
              startActivity(i11);
            }
            super.onPageStarted(view, url, favicon);
          }

          @Override
          public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            isPageLoaded = true;
            LogUtil.i(TAG, "页面加载完后==onPageFinished==" + url);

            myProgressDialog2.dismiss();
          }
        });
  }