如何调用从PhoneGap的一个Android活动 [英] How to call an Android Activity from PhoneGap

查看:146
本文介绍了如何调用从PhoneGap的一个Android活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的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.

我试过<一个href="http://stackoverflow.com/questions/2727763/communication-between-android-java-and-phonegap-javascript">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.

按照以下步骤。

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

  1. 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");
}

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

  • 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

    请提供您的意见在这里,以提高博客张贴。 <一href="http://phonegapexplorers.blogspot.in/2012/08/call-native-java-$c$c-phonegap-android.html">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

    这篇关于如何调用从PhoneGap的一个Android活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆