Reputation: 2725
I am new to Android and Phone gap and am trying to learn both at the same time. Below i am not only trying to create a plugin but also trying to see the flow of how control goes from one part to the other. You are not only allowed to tell me why my app does not outputs what i think it should but also you could question my logic and clear if i am doing it wrongly. Here is what i am trying to do
1: First i have the 'MainActivity.java' which extends from DroidGap
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
2: As you can see i am calling index.html from my java class. So, here is the 'index.html'.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
<script type="text/javascript" charset="utf-8" src="BreakitPlugin.js"></script>
<script type="text/javascript">
function onBodyLoad()
{
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady()
{
window.plugins.breakitPlugin.createNavigationBar();
}
</script>
</head>
<body onload="onBodyLoad()">
</body>
</html>
3: Then the script file
function BreakitPlugin() { }
BreakitPlugin.prototype.createNavigationBar = function() {
cordova.exec(null, null, "BreakitPlugin", "createNavigationBar", []); // Edited the fifth parameter
};
cordova.addConstructor(function(){
cordova.addPlugin("breakitPlugin", new BreakitPlugin());
});
4: and then the most important and crucial part. The plugin.java which extends from plugin. This is the part which does not seem to work. I have tried here to setContentView but it does not work. Also i have tried the log command but there are no corresponding log statements displayed at the runtime. So, both the functions in this class does not seem to be called. Bytheway, i have had 'alert' statements in my '.html' and '.js' and it seemed to be alerted properly. Here is the code.
public class BreakitPlugin extends Plugin {
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
Log.i("Execute", "Log Cat");
if (action.equals("createNavigationBar")) {
this.createNavigationBar();
}
else {
status = PluginResult.Status.INVALID_ACTION;
}
return new PluginResult(status, result);
}
public void createNavigationBar() {
//Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vogella.com"));
//Activity act = new Activity();
//act.setContentView(R.layout.activity_main);
Log.i("Create navigation bar", "Log Cat");
}
}
Upvotes: 0
Views: 445
Reputation: 23273
Well there are a few things wrong that I can see right off the bat:
1) Cordova should be cordova in your JS file.
2) Assuming BreakitPlugin is really in the package "com.example.breakitwebapp" then your plugin line should be:
<plugin name="BreakitPlugin" value="com.example.breakitwebapp.BreakitPlugin"/>
the name of the Plugin has to match the name you use on codova.exec.
3) "createNavigationBar" will never be called in Java. You have to do this in exec:
public PluginResult execute(String action, JSONArray args, String callbackId) {
if ("createNavigationBar".equals(action) {
Log.i("Log Cat", "Create navigation bar");
this.createNavigationBar();
}
return null;
}
4) Beyond that you aren't returning a PluginResult so the onSuccess or onFailure callbacks won't ever get called. Mind you they are both null in your example.
Upvotes: 2