gpt4 book ai didi

javascript - Android studio 从 sd 卡中的文件加载 javascript 函数

转载 作者:行者123 更新时间:2023-11-28 02:31:24 25 4
gpt4 key购买 nike

我正在使用 android 应用程序,该应用程序将在 sd 卡中写入文件 html。然后,在 Web View 中加载文件。应用程序可以加载 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 函数在应用程序中运行吗?

最佳答案

您调用的函数 openAndroidDialog() 未在 Javascript 接口(interface)中的任何位置定义。你必须调用 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>";

关于javascript - Android studio 从 sd 卡中的文件加载 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50459957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com