Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android項目中的assert文件下的html裡的js交互

Android項目中的assert文件下的html裡的js交互

編輯:關於Android編程

要是webview能夠與JavaScript交互,首先需要webview要啟用JavaScript:

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

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

      給webview添加JavaScript接口:

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

        本地JavaScript文件:

        [javascript] view plaincopy
        1. <script type="text/javascript">
        2. function showAndroidToast(toast) {
        3. Android.showToast(toast);
        4. }
        5. </script>

          整個代碼如下:

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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved