[英]Unable to call java methods from phonegap android?
我已经使用phonegap android从html字符串创建pdf文件创建了一个示例应用程序。我在java.Code中编写了用于创建PDF文件的代码,
public class PdfGenerator
{
private WebView mAppView;
private DroidGap mGap;
public PdfGenerator(DroidGap gap, WebView view)
{
mAppView = view;
mGap = gap;
}
public void generatePDF()
{
File root = Environment.getExternalStorageDirectory();
File gpxfile = new File(root, "test.pdf");
System.out.println("Path ::::"+gpxfile);
try{
Document document = new Document(PageSize.LETTER);
PdfWriter.getInstance(document, new FileOutputStream(gpxfile));
document.open();
document.addAuthor("Real Gagnon");
document.addCreator("Real's HowTo");
document.addSubject("Thanks for your support");
document.addCreationDate();
document.addTitle("Please read this");
HTMLWorker htmlWorker = new HTMLWorker(document);
String str = "<html><head></head><body>"+
"<a href='http://www.rgagnon.com/howto.html'><b>Real's HowTo</b></a>" +
"<h1>Show your support</h1>" +
"<p>It DOES cost a lot to produce this site - in ISP storage and transfer fees, " +
"in personal hardware and software costs to set up test environments, and above all," +
"the huge amounts of time it takes for one person to design and write the actual content." +
"<p>If you feel that effort has been useful to you, perhaps you will consider giving something back?" +
"<p>Donate using PayPal® to real@rgagnon.com." +
"<p>Contributions via PayPal are accepted in any amount " +
"<P><br><table border='1'><tr><td>Java HowTo<tr>" +
"<td bgcolor='red'>Javascript HowTo<tr><td>Powerbuilder HowTo</table>" +
"</body></html>";
htmlWorker.parse(new StringReader(str));
document.close();
System.out.println("Done");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
我的班级代码是
public class MainActivity extends DroidGap
{
PdfGenerator pdf;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
pdf = new PdfGenerator(this, appView);
appView.addJavascriptInterface(pdf, "PdfGenerator");
super.loadUrl("file:///android_asset/www/test.html");
}
}
Javascript代码
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script>
function pdf()
{
document.addEventListener("deviceReady", deviceReady, false);
}
function creatfile()
{
window.PdfGenerator.generatePDF();
}
function deviceReady() {
window.PdfGenerator.generatePDF();
}
</script>
</head>
<body>
<input type="submit" onclick="pdf()" value="IMEI" />
</body>
当我从javascript调用generatePDF时,出现以下错误,
Uncaught TypeError: Cannot call method 'generatePDF' of undefined
请帮我。
在DroidGap类内定义一个函数(不会)|(不会)|(不应)将该函数暴露给webview javascript环境,也不会通过Window将成员对象暴露为DOM对象。
您想要的(如Raymond和Larta所示)是编写一个PDF插件。
通常,您需要1.本机实现,这完全取决于您的插件所针对的平台。 如果是android,您可以找到本机插件
public class PDFWriter extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("generatePDF")) {
// CALL YOUR GENERATE PDF CODE HERE
return true;
}
return false;
}
}
successCB-是成功回调函数failCB-是失败回调函数 PDFWriter-是您正在调用的本机插件实现generatePDF-是您正在调用args的本机插件函数-要传递给本机插件函数的参数数组
请注意,这是一个完整的示例,我省略了JAVA中的import语句以及必要的钩子。 使用plugman npm模块为您自动生成这些。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.