If we want Call Android Activity from Java Script while Using PhoneGap.In this Scenario you need One Phone Gap Plugin.
Here i explain In details How to Start and Implementation.
HTML Page:
-------------
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/cordova-2.0.0.js"></script>
<script type="text/javascript">
function callPlugin()
{
HelloPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler,text);
}
var HelloPlugin = {
callNativeFunction: function (success, fail, resultType) {
return cordova.exec( success, fail, "HelloPlugin", "nativeAction", [resultType]);
//In the above method "HelloPlugin" very Usefull.Its refer to The our Plugin class.This parameter are used in //cordova.xml or plugin.xml file
//"nativeAction" is Action name its used in Plugin
//"resultType" is Responce vlaues.
}
};
//This Method Executes after Success
function nativePluginResultHandler (result) {
}
//This method executes if any error occured while executing the plugin.
function nativePluginErrorHandler (error) {
}
</script>
</head>
<body>
<form>
<input type="button" value="Call PhoneGap Plugin" onClick="callPlugin()"/>
</form>
</body>
</html>
XML FILE:
-----------
In res/xml folder have the xml files. Here you integrate your plugin class and Reference of the plugin class (Declared in javascript function cordova.exec( success, fail, "HelloPlugin", "nativeAction", [resultType]);)
config.xml:
=======
you create one more plugin tag here
<plugin name="HelloPlugin" value="com.test.plugin..HelloPlugin"/> //colored thing is your plugin class
Plugin class:
-----------
package com.test.plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
import android.util.Log;
public class HelloPlugin extends Plugin {
public static final String NATIVE_ACTION_STRING="nativeAction";
public static final String SUCCESS_PARAMETER="success";
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
Log.d("HelloPlugin", "Hello, this is a native function called from PhoneGap/Cordova!");
//only perform the action if it is the one that should be invoked
if (NATIVE_ACTION_STRING.equals(action)) {
Intent ie = null;
String buy_data = null;
try {
buy_data=data.getString(0);
}
catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
}
Now its working fine
No comments:
Post a Comment