javascript - 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 功能在应用程序中工作吗?
解决方案
您正在调用未在 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>";
推荐阅读
- javascript - 在 Azure Functions / Javascript 中反序列化 DeepObject 查询字符串键
- react-router - 从子节点获取父路由路径(react-router-V6)
- java - 密钥交换后的 Netty StreamCorruptedException
- flutter - 比较两个 json 对象列表并从 dart 中的第一个 json 对象列表中删除公共对象
- c# - 我可以在 VS 2019 中构建 .NET Core 3.1 应用程序并在 Ubuntu 上使用 mono 运行吗?
- javascript - NodeJS/Express 如何同时处理多个 CPU 绑定请求
- node.js - 使用 ec2 实例 T2 micro 流式传输视频
- asp.net - 从 Signtool 中删除 URL
- java - 如何使用 Java API 访问 YouTube 观看次数
- node.js - 带有聊天记录的安全消息系统