我在android中有一个应用程序,在phonegap中制作,在后台运行。我想在javascript验证后把它带到前台。我找到了这个链接(Communication between Android Java and Phonegap Javascript?),我可以在没有问题的情况下调用java中的方法。
我正在尝试改变它以满足我的需求。但是没有工作。
以下是我到目前为止所得到的:
Class Main:
public class HiveTaxi extends DroidGap {
private PhoneNumber mc;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
mc = new PhoneNumber(this, appView);
appView.addJavascriptInterface(mc, "MyPhone");
super.loadUrl(Config.getStartUrl());
}
//other code....
Class PhoneNumber:
public class PhoneNumber
{
private WebView mAppView;
private DroidGap mGap;
public PhoneNumber(DroidGap gap, WebView view)
{
mAppView = view;
mGap = gap;
}
@JavascriptInterface
public void getTelephoneNumber(Context context)
{
Intent it = new Intent("android.intent.action");
it.setComponent(new ComponentName(context.getPackageName(), HiveTaxi.class.getName()));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(it);
}
}
JS档案(第491行)
window.MyPhone.getTelephoneNumber();
错误:
03-09 18:55:52.939 32758-32758/com.projeto.hivetaxi D/CordovaLog﹕ file:///android_asset/www/js/functions.js: Line 491 : Uncaught Error: Error calling method on NPObject. 03-09 18:55:52.939 32758-32758/com.projeto.hivetaxi I/chromium﹕ [INFO:CONSOLE(491)] "Uncaught Error: Error calling method on NPObject.", source: file:///android_asset/www/js/functions.js (491)