【问题标题】:Unable to call java methods from phonegap android?无法从phonegap android调用java方法?
【发布时间】:2014-07-25 11:15:47
【问题描述】:

我已经使用 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();
    }
    }
}

我的mian类代码是,

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 

请帮帮我。

【问题讨论】:

  • 我建议你在谷歌搜索。此外,我很确定这个问题已经得到解答。这不是在js代码中调用java函数的方式。有一个函数调用cordova.exec。请看一下文档。
  • 同上。请参阅插件文档:cordova.apache.org/docs/en/3.5.0/…
  • 我只从谷歌得到代码。它实际上有时工作正常

标签: java javascript android cordova


【解决方案1】:

在 DroidGap 类中定义一个函数(不)|(不会)|(不应该)将该函数暴露给 webview javascript 环境,也不会通过 Window 将成员对象暴露为 DOM 对象。

您想要(如 Raymond 和 Larta 所指出的)是编写一个 PDF 插件。

Here is the documentation for writing a Cordova Plugin

一般来说,你需要 1. 原生实现 这完全取决于您的插件所针对的平台。在android的情况下,你可以找到native plugin

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;
    }
}
  1. JS 实现 你的可能看起来像 PDFWriter.js: 科尔多瓦.exec(成功CB, 失败CB, "PDFWriter", “生成PDF”, 参数);

successCB - 是成功回调 failCB - 是失败回调 PDFWriter - 是您调用的本机插件实现 generatePDF - 是您调用的原生插件函数 args - 要传递给原生插件函数的参数数组

请注意,这是一个完整的示例,我省略了 JAVA 中的导入语句以及必要的钩子。使用 plugman npm 模块为您自动生成这些。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    相关资源
    最近更新 更多