2013-05-09 42 views
0

我一直試圖讓這個工作在過去的幾個小時內完成。我有科爾多瓦2.2。Phonegap Cordova 2.2插件開發,無法在Java和JavaScript之間進行通信

我創建了一個名爲com.tester.newvideouri的新包。

我有一個在這個包稱爲newVideoUri類,內容如下

package com.tester.newvideouri; 

import org.apache.cordova.api.CallbackContext; 
import org.apache.cordova.api.CordovaPlugin; 
import org.json.JSONArray; 
import org.json.JSONException; 

/** 
* This class echoes a string called from JavaScript. 
*/ 
public class newVideoUri extends CordovaPlugin { 
    @Override 
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
     if (action.equals("echo")) { 
      String message = args.getString(0); 
      this.echo(message, callbackContext); 
      return true; 
     } 
     return false; 
    } 

    private void echo(String message, CallbackContext callbackContext) { 
     System.out.println("success here and display it"); 
     if (message != null && message.length() > 0) { 
      callbackContext.success(message); 
     } else { 
      callbackContext.error("Expected one non-empty string argument."); 
     } 
    } 
} 

在我​​3210我添加以下行:

<plugin name="Echo" value="com.tester.newvideouri.newVideoUri" /> 

在我javascript文件我有以下幾點:

function onLoad() { 
     document.addEventListener("deviceready", onDeviceReady, false); 
    } 
    window.onDeviceReady = function() {   
     window.echo = function(str, callback) { 
      alert('Started'); 
      cordova.exec(callback, function(err) { 
       callback('Nothing to echo.'); 
      }, "Echo", "echo", [ str ]); 
      alert('The END'); 
     };   
     window.echo("echome", function(echoValue) { 
      alert(echoValue == "echome"); // should alert true. 
     }); 
    } 

當我運行代碼時沒有任何反應。任何人都可以告訴我我做錯了什麼?

回答

0

寫 「echome」 代碼到這樣的功能:

function echome(){ 
    window.echo("echome", function(echoValue){ 
    alert(echoValue == "echome"); 
    }); 
} 

,並呼籲在HTML代碼此功能是這樣的:

<a href="javascript:echome();">Click here</a> 

嘗試運行它。它爲我工作。 :)

0

代碼是按照本例正確的:http://docs.phonegap.com/en/2.6.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android

可能是你的問題是,科爾多瓦的原因是沒有「鉤」了。你確實添加了cordova.android.js到你的網頁上嗎?

如果是這樣,確保設備就緒實際上是被稱爲:

window.onDeviceReady = function() { 
    alert('ready to work'); 
    // if you do not see this alert nothing in cordova will work as 
    // it means communication was not started 

} 
0

你需要在你的JavaScript文件中輸入:

cordova.define("cordova/plugin/sampleplugin", function(require, exports, module) { 
    var exec = require('cordova/exec'); 
    var SamplePlugin = function() {}; 

    SamplePlugin.prototype.authenticate = function(loginId,plainTextPassword,successCallback,failureCallback) { 
     return exec(successCallback, failureCallback, 'SamplePlugin', 'authenticate', [loginId,plainTextPassword]); 

    } 
     var sampleplugin = new SamplePlugin(); 
     module.exports = sampleplugin; 
}); 

這裏SamplePlugin是我的插件的名稱。

而且在HTML文件中,寫這樣的代碼:

var sampleplugin = cordova.require("cordova/plugin/sampleplugin"); 
      function callrestfulweb() 
{ 
       var loginId= document.getElementById("loginId").value; 
       var plainTextPassword = document.getElementById("plainTextPassword").value; 
       sampleplugin.authenticate(loginId,plainTextPassword,function(){alert("login successful");},function(){alert("Invalid username or password ");}) 
     } 

我希望你可以通過這個例子瞭解。

+0

您還可以使用此鏈接http://simonmacdonald.blogspot.ca/2013/01/galleryplugin.html – prateek 2013-09-25 11:23:44

0

你需要在你的JavaScript文件中輸入:

cordova.define("cordova/plugin/sampleplugin", function(require, exports, module) { 
    var exec = require('cordova/exec'); 
    var SamplePlugin = function() {}; 

    SamplePlugin.prototype.authenticate = function(loginId,plainTextPassword,successCallback,failureCallback) { 
     return exec(successCallback, failureCallback, 'SamplePlugin', 'authenticate', [loginId,plainTextPassword]); 

    } 
     var sampleplugin = new SamplePlugin(); 
     module.exports = sampleplugin; 
}); 

這裏SamplePlugin是我

而且在HTML文件中,寫這樣的代碼名稱

var sampleplugin = cordova.require("cordova/plugin/sampleplugin"); 
      function callrestfulweb() 
{ 
       var loginId= document.getElementById("loginId").value; 
       var plainTextPassword = document.getElementById("plainTextPassword").value; 
       sampleplugin.authenticate(loginId,plainTextPassword,function(){alert("login successful");},function(){alert("Invalid username or password ");}) 
     } 

希望你可以通過這個例子來理解。

相關問題