Android工作室从SD卡中的文件加载javascript函数

时间:2018-05-22 04:46:20

标签: javascript android html

我正在使用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功能在应用程序中运行吗?

1 个答案:

答案 0 :(得分:0)

你正在调用函数openAndroidDialog(),它没有在Javascript接口的任何地方定义。你必须调用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>";