I need to open new activity and do other functions like display a Toast in native android java , when button in HTML is clicked. I found a similar question and solution with the problem I'm having, but unfortunately the answer doesn't help. Start new Activity when html button clicked in Cordova
Here is the log:
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
New Log using 'extends 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 }
And here is my 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();
}
}
}
And in my 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>
You should have a look at Cordova plugins, as it provides a standard way of accessing the native functionality of the device. It is preferable to baking in your custom way.
Closer to this topic, it might be that this plugin is exactly what you want.
User contributions licensed under CC BY-SA 3.0