首页 > 解决方案 > Android Studio 从 sd 卡中的文件加载 javascript 函数

问题描述

我正在使用 android 应用程序,该应用程序将在 sd 卡中写入文件 html。然后,在 Web 视图中加载文件。应用程序可以加载 html 文件,但 html 文件中的 javascript 函数无法正常工作。下面是我正在处理的代码。

public class WriteWebPage extends AppCompatActivity {

Button btn;
WebView wb;

private String fileName = "SampleFile.html";
private String filePath = "/sdcard/";
File myExternalFile;
String myData = "";

@SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.read_webpage);

    btn = findViewById(R.id.btn);
    wb = findViewById(R.id.wb);

    final MyJavaScriptInterface myJavaScriptInterface = new MyJavaScriptInterface(this);
    wb.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");

    wb.getSettings().setJavaScriptEnabled(true);
    wb.loadUrl("file://" + myExternalFile);

    wb.setWebChromeClient(new WebChromeClient());
    wb.setWebViewClient(new WebViewClient());


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {

                String a = "asd12sf";
                String html = "<html><body>" +
                        "<h3>Hello World</h3>" +
                        "<p>" + a  + "</p>" +
                        "<input type= 'button' value='button' onClick='dialog()'/>" +
                        "<script language='javascript'>" +
                        "function dialog(){" +
                        "AndroidFunction.openAndroidDialog();}" +
                        "</script>" +
                        "</body></html>";

                FileOutputStream fos = new FileOutputStream(myExternalFile);
                fos.write(html.getBytes());
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

              wb.loadUrl("file://"+myExternalFile);

        }
    });

    if(isExternalStorageAvailable()){
        myExternalFile = new File(getExternalFilesDir(filePath), fileName);
    }
}

private static boolean isExternalStorageAvailable(){

    String extStorageState = Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED.equals(extStorageState)){
        return true;
    }
    return false;
}

private class  MyJavaScriptInterface{

    Context mContext;

    MyJavaScriptInterface(Context c){
        mContext = c;
    }
    @JavascriptInterface
    public void dialog(){
        AlertDialog.Builder myDialog = new AlertDialog.Builder(WriteWebPage.this);
        myDialog.setTitle("Danger!");
        myDialog.setMessage("Hi");
        myDialog.setPositiveButton("ON", null);
        myDialog.show();
    }
}
}

有人可以帮助我使 javascript 功能在应用程序中工作吗?

标签: javascriptandroidhtml

解决方案


您正在调用未在 Javascript 界面中的任何位置定义的函数 openAndroidDialog()。你必须调用 dialog() 函数

你的 html

 String html = "<html><body>" +
                    "<h3>Hello World</h3>" +
                    "<p>" + a  + "</p>" +
                    "<input type= 'button' value='button' onClick='dialog()'/>" +
                    "<script language='javascript'>" +
                    "function dialog(){" +
                    "AndroidFunction.openAndroidDialog();}" +
                    "</script>" +
                    "</body></html>";

应该

  String html = "<html><body>" +
                    "<h3>Hello World</h3>" +
                    "<p>" + a  + "</p>" +
                    "<input type= 'button' value='button' onClick='dialog()'/>" +
                    "<script language='javascript'>" +
                    "function dialog(){" +
                    "AndroidFunction.dialog();}" +
                    "</script>" +
                    "</body></html>";

推荐阅读