Sie können ein benutzerdefiniertes Plugin erstellen, um eine Methode von der nativen Seite aufzurufen. Erstellen Sie eine separate JavaScript-Datei, sagen customplugin.js, und setzen diese hinein:
var CustomPlugin = {};
CustomPlugin.callNativeMethod = function() {
cordova.exec(null, null, "CustomPlugin", "callNativeMethod", []);
};
nun auf der nativen Java-Seite, erstellen Sie eine neue Klasse und nennen Sie es CustomPlugin.java, dann fügen Sie diese:
package com.yourpackage;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.CordovaPlugin;
import com.yourpackage.MainActivity;
public class CustomPlugin extends CordovaPlugin
{
private static final String TAG = "CustomPlugin";
private CallbackContext callbackContext = null;
private MainActivity activity = null;
/**
* Override the plugin initialise method and set the Activity as an
* instance variable.
*/
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView)
{
super.initialize(cordova, webView);
// Set the Activity.
this.activity = (MainActivity) cordova.getActivity();
}
/**
* Here you can delegate any JavaScript methods. The "action" argument will contain the
* name of the delegated method and the "args" will contain any arguments passed from the
* JavaScript method.
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException
{
this.callbackContext = callbackContext;
Log.d(TAG, callbackContext.getCallbackId() + ": " + action);
if (action.equals("callNativeMethod"))
{
this.callNativeMethod();
}
else
{
return false;
}
return true;
}
private void callNativeMethod()
{
// Here we simply call the method from the Activity.
this.activity.callActivityMethod();
}
}
Achten Sie darauf, die Plug-ins in der Datei config.xml Karte, indem Sie diese Zeile hinzufügen:
...
<feature name="CustomPlugin">
<param name="android-package" value="com.yourpackage.CustomPlugin" />
</feature>
...
nun das Plugin von der index.html rufen Sie einfach Ihre JavaScript-Methode aufrufen können:
CustomPlugin.callNativeMethod();
Mit dieser Methode können Sie viele benutzerdefinierte Methoden bequem einrichten. Weitere Informationen finden Sie im PhoneGap Plugin Entwicklungshandbuch here.
Dank Arbeit zu machen @suprnova Ihre Antwort ist hilfreich vielleicht funktioniert es für vorherige Version, aber ich merke, dass ich die letzte Version von phonegap (3.0) benutze und ich habe das kodiert, aber funktioniert nicht. Ich habe diesen Fehler exec() Aufruf zu unbekannten Plugin, ich fand etwas ähnliches hier http://stackoverflow.com/questions/17974301/phonegap-3-plugin-exec-call-to-unknown-plugin – nramirez
Sie haben Recht, Das funktioniert nicht mit PhoneGap 3.0, aber ich denke, das liegt daran, dass das Plugin-Mapping, das ich in der vorherigen Antwort verwendet habe, veraltet ist. Allerdings habe ich meine Antwort mit dem PhoneGap 3.0 Mapping aktualisiert. Im Wesentlichen ist das Element "" in der Datei config.xml veraltet, Sie müssen stattdessen das Element "" verwenden. –
kieranroneill
Ja! Ich arbeite daran! Sobald ich die Lösung für diese Frage gefunden habe, werde ich die Antwort posten! – nramirez