android webview html5交互的问题

天王盖地虎626 发布于 2015/04/21 16:08
阅读 1K+
收藏 0

 

wv.addJavascriptInterface(new Object(){
   public String captureOperate(final String fileName,final String flag){
    mHandler.post(new Runnable(){

     @Override
     public void run() {
      // TODO Auto-generated method stub
     try{
      
      if("8".equals(flag)){
       
       path =path.replaceFirst("file://", "");
                      File uploadFile= new File(path);
                      if(uploadFile.exists()){
                       ftpManager.ftpUploadFile(path);
                          }else{
                       Log.e("MonitManagement-MainActivity-onCreate-addJavascriptInterface-captureOperate","file not exist");
                      }
       
      }
      
     }catch(Exception e){
      
      Log.e("MonitManagement-MainActivity-captureOperate:", "MainActivity captureOperate");
       
      }
     }
     
    });
    return fullFileName; //fullFileName是MainActivity类的公共变量成员
   }
   
   
  }, "MyBrowserAPI");

我现在遇到一个问题,在webview中内嵌的网页中,通过js方法希望调用这个android中设置的方法,却发现,只有部分手机能调用的到,部分手机调用不到,请问这是怎么回事呀?

加载中
1
寒天
寒天

在android4.2以前,注入步骤如下:

  1. webview.getSetting().setJavaScriptEnable(true);  
  2. class JsObject {  
  3.     public String toString() { return "injectedObject"; }  
  4.  }  
  5.  webView.addJavascriptInterface(new JsObject(), "injectedObject");  


Android4.2及以后,注入步骤如下:
  1. webview.getSetting().setJavaScriptEnable(true);  
  2. class JsObject {  
  3.     @JavascriptInterface  
  4.     public String toString() { return "injectedObject"; }  
  5.  }  
  6.  webView.addJavascriptInterface(new JsObject(), "injectedObject");  

From the Android 4.2 documentation:

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher

0
天王盖地虎626
天王盖地虎626
没有人知道吗?
0
天王盖地虎626
天王盖地虎626

引用来自“寒天”的评论

在android4.2以前,注入步骤如下:

  1. webview.getSetting().setJavaScriptEnable(true);  
  2. class JsObject {  
  3.     public String toString() { return "injectedObject"; }  
  4.  }  
  5.  webView.addJavascriptInterface(new JsObject(), "injectedObject");  


Android4.2及以后,注入步骤如下:
  1. webview.getSetting().setJavaScriptEnable(true);  
  2. class JsObject {  
  3.     @JavascriptInterface  
  4.     public String toString() { return "injectedObject"; }  
  5.  }  
  6.  webView.addJavascriptInterface(new JsObject(), "injectedObject");  

From the Android 4.2 documentation:

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher

非常感谢
返回顶部
顶部