在Cordova中的HTML中单击Button时启动新活动

时间:2015-04-13 05:10:49

标签: javascript android html cordova phonegap-build

当点击HTML中的按钮时,我需要打开新活动并执行其他功能,例如在本机android java中显示Toast。我找到了一个与我遇到的问题类似的问题和解决方案,但遗憾的是答案并没有帮助。 Start new Activity when html button clicked in Cordova

这是日志:

04-13 11:21:00.080: D/WebViewCallback(23907): onConsoleMessage
04-13 11:21:00.080: D/CordovaLog(23907): file:///android_asset/www/index.html: Line 13 : Uncaught ReferenceError: MyJSInterface is not defined

使用&#39的新日志扩展了CordovaActivity':

04-14 10:21:33.544: W/ResourceType(18650): No package identifier when getting name for resource number 0x00000064
04-14 10:21:33.545: W/ResourceType(18650): No package identifier when getting name for resource number 0x00000064
04-14 10:21:34.229: W/ResourceType(18650): No package identifier when getting name for resource number 0x00000064
04-14 10:21:34.231: I/View(18650): Touch down dispatch to org.apache.cordova.CordovaWebView{41cdc4d8 VFEDH.C. .F...... 0,0-720,1230 #64}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=52.92649, y[0]=183.81734, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=5511466, downTime=5511466, deviceId=4, source=0x1002 }
04-14 10:21:34.372: W/ResourceType(18650): No package identifier when getting name for resource number 0x00000064
04-14 10:21:34.372: I/View(18650): Touch up dispatch to org.apache.cordova.CordovaWebView{41cdc4d8 VFEDH.C. .F...... 0,0-720,1230 #64}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=52.92649, y[0]=183.81734, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=5511610, downTime=5511466, deviceId=4, source=0x1002 }

这是我的Java

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import org.apache.cordova.*;

public class MainActivity extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();

        appView.addJavascriptInterface(new MyJSInterface(),
                "myJSInterface");
        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);
    }

  //JSInterface class
    public class MyJSInterface {

        public MyJSInterface() {
            // TODO Auto-generated constructor stub
            Log.i(TAG, "constructor of jsinterface");
        }

        public void btnClick() {
            //do something
            Log.i(TAG, "HELLO");
            Toast.makeText(getApplicationContext(), "HELLO", Toast.LENGTH_SHORT).show();
        }
    }
}

在我的HTML中

<!DOCTYPE HTML>
<html>
 <head>
  <title>PhoneGap</title>
  <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
 </head>
 <body>
    <h1>Hello PhoneGap</h1>
    <button onclick="callFunction()">test</button>
 </body>
 <script>
function callFunction(){
    MyJSInterface.btnClick();
}
</script>
</html>

1 个答案:

答案 0 :(得分:1)

您应该查看Cordova plugins,因为它提供了访问设备本机功能的标准方法。最好以自定义方式烘焙。

更接近这个主题,可能是plugin正是你想要的。