我相信可以从(PhoneGap)Javascript调用Java方法。
有人知道该怎么做吗?(我知道如何通过更改PhoneGap的源代码来做到这一点,但我会避免这样做)
Answers:
我终于做到了。
使用您要使用的方法创建一个类:
public class MyClass {
private WebView mAppView;
private DroidGap mGap;
public MyClass(DroidGap gap, WebView view)
{
mAppView = view;
mGap = gap;
}
public String getTelephoneNumber(){
TelephonyManager tm =
(TelephonyManager) mGap.getSystemService(Context.TELEPHONY_SERVICE);
String number = tm.getLine1Number();
return number;
}
}
在您的主要活动中,为该类添加一个Javascript接口:
public class Main extends DroidGap
{
private MyClass mc;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
mc = new MyClass(this, appView);
appView.addJavascriptInterface(mc, "MyCls");
super.loadUrl(getString(R.string.url));
}
}
在Javascript调用window.MyCls方法中:
<script>
$(function(){
$("#phone").text("My telephone number is: " +
window.MyCls.getTelephoneNumber());
});
</script>
注意:
如评论中所述,对于Android 4.2及更高版本,将其添加@JavascriptInterface
到要从HTML页面访问的方法中。参考。
addJavaScriptInterface(mc, "MyCls")
没有Gap init()
ed可能会导致应用程序崩溃,您最好super.init()
在添加之前addJavascriptInterface()
public class Main extends DroidGap
{
private MyClass mc;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
mc = new MyClass(this, appView);
appView.addJavascriptInterface(mc, "MyCls");
super.loadUrl(getString(R.string.url));
}
}
PhoneGap有一个不错的插件API。您将通过实现IPlugin接口用Java编写插件。大多数魔术都在execute()函数中。
public interface IPlugin {
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/
PluginResult execute(String action, JSONArray args, String callbackId);
// ... more ...
}
开始编写插件的最佳方法是首先编写javascript API。通常,您首先要编写一个自定义javascript类,然后在javascript类的每个方法中,将变量编组并调用使用Phonegap.exec()方法开发的插件。这是方法签名,供您参考。
/* src/com/phonegap/api/PluginManager.java */
/**
* Receives a request for execution and fulfills it by finding the appropriate
* Java class and calling it's execute method.
*
* PluginManager.exec can be used either synchronously or async. In either case, a JSON encoded
* string is returned that will indicate if any errors have occurred when trying to find
* or execute the class denoted by the clazz argument.
*
* @param service String containing the service to run
* @param action String containt the action that the class is supposed to perform. This is
* passed to the plugin execute method and it is up to the plugin developer
* how to deal with it.
* @param callbackId String containing the id of the callback that is execute in JavaScript if
* this is an async plugin call.
* @param args An Array literal string containing any arguments needed in the
* plugin execute method.
* @param async Boolean indicating whether the calling JavaScript code is expecting an
* immediate return value. If true, either PhoneGap.callbackSuccess(...) or
* PhoneGap.callbackError(...) is called once the plugin code has executed.
*
* @return JSON encoded string with a response message and status.
*/
@SuppressWarnings("unchecked")
public String exec(final String service, final String action,
final String callbackId, final String jsonArgs,
final boolean async)
您还需要注册插件。为此,您可以在自定义javascript库的底部添加注册代码。
在下面的示例中,作者定义了一个javascript BarcodeScanner类,并使用addConstructor方法对其进行注册。
在addConstructor中执行两个步骤:
在javascript中创建BarcodeScanner的新实例并进行注册。这可以在javascript中作为window.plugins.barcodeScanner访问
用服务名称注册自定义插件类。该服务名称作为第一个参数传递给PhoneGap.exec,以便PhoneGap可以实例化Java插件类并在其上调用execute()方法。
样本注册码:
PhoneGap.addConstructor(function() {
/* The following registers an instance of BarcodeScanner in window.plugins.barcodeScanner */
PhoneGap.addPlugin('barcodeScanner', new BarcodeScanner());
/* The following associates a service name BarcodeScanner with a class com.beetight.barcodescanner.BarcodeScanner */
/* The service name is the first argument passed into PhoneGap.exec */
PluginManager.addService("BarcodeScanner","com.beetight.barcodescanner.BarcodeScanner");
});
一个简单的形式:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
super.appView.getSettings().setJavaScriptEnabled(true);
super.appView.addJavascriptInterface(this, "MyCls");
super.loadUrl("file:///android_asset/www/login.html");
}
如果有人使用上面的代码获取了nullPointer异常,请先执行super.oncreate(),然后再执行super..init()
super.onCreate(savedInstanceState);
super.init();
我在这里找到此解决方案:Phonegap Google Group
非常感谢@ zorglub76提供的解决方案。...