Android项目中的assert文件下的html里的js交互

要是webview能够与JavaScript交互,首先需要webview要启用JavaScript:

  1. WebSettings webSettings = myWebView.getSettings();  
  2.         webSettings.setJavaScriptEnabled(true);  
 然后创建JavaScript的接口:

  1. public class WebAppInterface {  
  2.         Context mContext;  
  3.   
  4.         /** Instantiate the interface and set the context */  
  5.         WebAppInterface(Context c) {  
  6.             mContext = c;  
  7.         }  
  8.   
  9.         /** Show a toast from the web page */  
  10.         @JavascriptInterface  
  11.         public void showToast(String toast) {  
  12.             Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();  
  13.         }  
  14.     }  

给webview添加JavaScript接口:

[html]  view plain copy
  1. myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");  

  本地JavaScript文件:

[javascript]  view plain copy
  1. <input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />  
  2.   
  3. <script type="text/javascript">  
  4.     function showAndroidToast(toast) {  
  5.         Android.showToast(toast);  
  6.     }  
  7. </script>  

  整个代码如下:

[java]  view plain copy
  1. public class MainActivity extends Activity {  
  2.     private WebView myWebView;  
  3.   
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_main);  
  8.         myWebView = (WebView) findViewById(R.id.webview);  
  9.         WebSettings webSettings = myWebView.getSettings();  
  10.         webSettings.setJavaScriptEnabled(true);  
  11.         myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");  
  12.         ProcessWebString();  
  13.   
  14.     }  
  15.   
  16.     public class WebAppInterface {  
  17.         Context mContext;  
  18.   
  19.         /** Instantiate the interface and set the context */  
  20.         WebAppInterface(Context c) {  
  21.             mContext = c;  
  22.         }  
  23.   
  24.         /** Show a toast from the web page */  
  25.         @JavascriptInterface  
  26.         public void showToast(String toast) {  
  27.             Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();  
  28.         }  
  29.     }  
  30.   
  31.     private void ProcessWebString() {  
  32.         // 加载 asset 文件  
  33.         String tpl = getFromAssets("web_tpl.html");  
  34.         myWebView.loadDataWithBaseURL(null, tpl, "text/html""utf-8"null);  
  35.     }  
  36.   
  37.     /* 
  38.      * 获取html文件 
  39.      */  
  40.     public String getFromAssets(String fileName) {  
  41.         try {  
  42.             InputStreamReader inputReader = new InputStreamReader(  
  43.                     getResources().getAssets().open(fileName));  
  44.             BufferedReader bufReader = new BufferedReader(inputReader);  
  45.             String line = "";  
  46.             String Result = "";  
  47.             while ((line = bufReader.readLine()) != null)  
  48.                 Result += line;  
  49.             return Result;  
  50.         } catch (Exception e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.         return "";  
  54.     }  
  55.   
  56. }  
这里获取到的是assert文件下的html字符串,写了一个方法获取assert文件下html


还有种方法可以直接load

webView.loadUrl("file:///android_asset/html/company.html");

assets文件下的html文件下的company.html文件


webView.setWebChromeClient(new MyWebClient());

private class MyWebClient extends WebChromeClient {


@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// TODO Auto-generated method stub
Intent intent = null;
if (message.contains("tid")) {
Pattern pattern = Pattern.compile("tid=(\\d+)");
Matcher matcher = pattern.matcher(message);
String tid = "";
while (matcher.find()) {
String st = matcher.group(0);
tid = st.split("=")[1];
System.out.println("---" + tid);
}
intent = new Intent(BbsMyHomeActivity.this,
BbsHomeDetailTestActivity.class);
intent.putExtra("tid", tid);
} else {
intent = new Intent(BbsMyHomeActivity.this,
MyAttentionDetailActivity.class);
intent.putExtra("uid", message);
}
startActivity(intent);
// new CustomeToast(BbsMyHomeActivity.this, message);
new PopupToast(BbsMyHomeActivity.this, message, footlayout);
// 少写了这行代码可能会导致,点击了一次js里的alert弹出方法,无法点击第二次。
result.cancel();
return true;
}
}


http://blog.csdn.net/ithomer/article/details/8737999


做项目的时候还发现一个问题,就是WebView的addJavascriptInterface方法失效的问题

里面的类的方法名上面要加annotation
@JavascriptInterface

表忘了导包import android.webkit.JavascriptInterface;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值