如何调用从PhoneGap的一个Android活动PhoneGap、Android

由网友(就像个笑话)分享简介:我是新来的PhoneGap和我能够实现与PhoneGap的基本应用程序,现在要进一步加强它,我想与Android活动连接的PhoneGap,基本上是我的计划是使用JavaScript函数调用startActivity()方法I am new to PhoneGap and I am able to implement...

我是新来的PhoneGap和我能够实现与PhoneGap的基本应用程序,现在要进一步加强它,我想与Android活动连接的PhoneGap,基本上是我的计划是使用JavaScript函数调用startActivity()方法

I am new to PhoneGap and I am able to implement the basic app with PhoneGap, now to enhance it further, I want to connect PhoneGap with Android Activities, basically what I plan is to call startActivity() method using a javascript function.

我试过Communication Android的Java和Javascript的的PhoneGap之间?

但我没有打电话的活动,从而导致强制关闭错误。 请帮我出,等待答复!

but I failed to call an activity, causing force close error. Do help me out, awaiting a reply!

推荐答案

任何Java本地code调用被调用,而无需使用任何插件如下。

Any Java Native code call be called without using any plugin as following.

按照以下步骤。

更​​换以下code与您现有的DroidGap活动。

Replace the following code with your existing DroidGap Activity.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init(); // Calling this is necessary to make this work
    appView.addJavascriptInterface(this, "MainActivity");

    /* "this" points the to the object of the current activity. "MainActivity" is used to refer "this" object in JavaScript as in Step 3. */

    super.loadUrl("file:///android_asset/www/index.html");
}

在当前(本)活动添加自定义函数如下。

如何使用Android调用百度地图API

Add the custom function in current (this) activity as following.

public void customFunctionCalled() {
    Log.e("Custom Function Called", "Custom Function Called");
}

现在从你的HTML / JavaScript的code调用这个函数如下。

Now call this function from your HTML/JavaScript code as following.

<script type="text/javascript">
    function callNewActivity() {
        window.MainActivity.customFunctionCalled();
    }
</script>

这将调用 customFunctionCalled() MainActivity

测试环境 日食 - 3.7.2 Android 2.2的模拟器 PhoneGap的 - 2.0.0

Tested Environment Eclipse - 3.7.2 Android 2.2 Emulator PhoneGap - 2.0.0

请提供您的意见在这里,以提高博客张贴。 http://phonegapexplorers.blogspot.in/2012/08/call-native-java-$c$c-phonegap-android.html

Please provide your comments here to improve blogs post. http://phonegapexplorers.blogspot.in/2012/08/call-native-java-code-phonegap-android.html

阅读全文

相关推荐

最新文章