4.webView的ajax或javascript响应

1.设置支持javaScript


WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);

 

2.如果想查bug设置

wv.setWebContentsDebuggingEnabled(true);

 

3.webview加请求头

final Map<String, String> map = newHashMap<String, String>();

       map.put("assetionkey",StringUtils.getBase64(RetrofitHelper.key + timeString));

       map.put("timestamp", timeString);

       map.put("managerid", managerId + "");

 

wv.loadUrl(url1, map);

 

4.webView支持ajax弹框

 

wv.setWebChromeClient(new WebChromeClient() {
                                 
// 拦截输入框(原理同方式2)
                                  // 参数message:代表promt()的内容(不是url)
                                  // 参数result:代表输入框的返回值
                                  @Override
                                 
public boolean onJsPrompt(WebViewview, String url, String message, StringdefaultValue, final JsPromptResult result) {
                                     
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());

                                     
builder.setTitle("").setMessage(message);

                                     
final EditText et = new EditText(view.getContext());
                                     
et.setSingleLine();
                                     
et.setText(defaultValue);
                                     
builder.setView(et)
                                       
      .setPositiveButton("确定",new DialogInterface.OnClickListener() {
                                                 
public void onClick(DialogInterface dialog, int which){
                                                     
wv.loadUrl(url1, map);
 
                                                    result.confirm(et.getText().toString());

                                                 
}

                                             
})
                                             .setNeutralButton("取消", new DialogInterface.OnClickListener(){
                                                 
public void onClick(DialogInterface dialog, int which){
                                                     
result.cancel();
                          
                       }
                                             
});

                                     
// 屏蔽keycode等于84之类的按键,避免按键后导致对话框消息而页面无法再弹出对话框的问题
                                      builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
                                         
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                                             
Log.v("onJsPrompt", "keyCode==" +keyCode + "event=" + event);
                    
                         return true;
                                         }
                                     
});

                                     
// 禁止响应按back键的事件
                                      //builder.setCancelable(false);
                                      AlertDialog dialog = builder.create();
                                     
dialog.show();
                                     
dialog.getWindow().setLayout((int) dip2px(400), LinearLayout.LayoutParams.WRAP_CONTENT);
   
                                  return true;
                                      // return super.onJsPrompt(view, url, message,defaultValue,
                                     
//result);

                                  }

// 通过alert()和confirm()拦截的原理相同,此处不作过多讲述

                                  // 拦截JS的警告框
                                  @Override
                                 
public boolean onJsAlert(WebViewview, String url, String message, JsResultresult) {
                           
          final AlertDialog.Builderbuilder = new AlertDialog.Builder(view.getContext());

                                     
builder.setTitle("").setMessage(message).setPositiveButton("确定",null);

                                     
// 不需要绑定按键事件
  
                                   // 屏蔽keycode等于84之类的按键
                                      builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
                                         
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                                             
Log.v("onJsAlert", "keyCode==" +keyCode + "event=" + event);
                                             
return true;
                                         }
                        
             });
                                     
// 禁止响应按back键的事件
                                      builder.setCancelable(false);
                                     
AlertDialog dialog = builder.create();
                                     
dialog.show();
                                     
dialog.getWindow().setLayout((int) dip2px(400), LinearLayout.LayoutParams.WRAP_CONTENT);
                                     
result.confirm();// 因为没有绑定事件,需要强行confirm,否则页面会变黑显示不了内容。
                                      return true;
                                     
// return super.onJsAlert(view, url, message,result);

                                 
}

                                 
// 拦截JS的确认框
                                  @Override
    
                             public boolean onJsConfirm(WebViewview, String url, String message, final JsResultresult) {
                                     
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
              
                       builder.setTitle("")
                                             
.setMessage(message)
                                             .setPositiveButton("确定", new DialogInterface.OnClickListener(){
                                 
                public void onClick(DialogInterfacedialog, int which) {
                                                     
wv.loadUrl(url1, map);
                                                     
result.confirm();
                                 
                }
                                             
})
                                             .setNeutralButton("取消", new DialogInterface.OnClickListener(){
                                                 
public void onClick(DialogInterface dialog, int which){
                                                     
result.cancel();
                                                 
}
                                             
});
                                     
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                                         
@Override
                                         
public void onCancel(DialogInterface dialog) {
                                             
result.cancel();
                                         
}
                                     
});

                                     
// 屏蔽keycode等于84之类的按键,避免按键后导致对话框消息而页面无法再弹出对话框的问题
                                      builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
                                         
@Override
                                         
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                                             
Log.v("onJsConfirm", "keyCode==" +keyCode + "event=" + event);
                                             
return true;
                                         }
                                     
});
                                     
// 禁止响应按back键的事件
                                      //builder.setCancelable(false);
                                      AlertDialog dialog = builder.create();
                                     
dialog.show();
                                     
dialog.getWindow().setLayout((int) dip2px(400), LinearLayout.LayoutParams.WRAP_CONTENT);
                                     
return true;
                                      // return super.onJsConfirm(view, url,message, result);

                                 
}
             
                }
        );

 

5.webView释放

 

@Override
protected void onDestroy() {
   
super.onDestroy();
   
wv.removeAllViews();
   
wv.destroy();
}


阅读更多
上一篇3.Retrofit上传头像给后端
下一篇5、webView的缓存
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭