•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
1.
cordova create C:\MyFirstPlugin com.unal.plugins CustomPluginActivity
$cd C:\MyFirstPlugin
$cordova platform add android
$
•
•
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext)
throws JSONException
{
boolean validAction = true;
Log.d("Plugin ejecutando acción:",action);
if (action.equals("nativeAction"))
{
callbackContext.success(args.getString(0));
}
else
{
validAction = false;
}
return validAction;
}
•
<feature name="HelloPlugin">
<param name="android-package"
value="com.unal.plugins.plugin.HelloPlugin" />
</feature>
•
<!DOCTYPE html>
<html>
<head>
<title>Hola Plugin</title>
</head>
<body>
<h1>Prueba del nuevo Plugin!</h1>
<button onclick="callNativePlugin('success');">Click para invocar al PluginNativo con SUCCESS!</button>
<button onclick="callNativePlugin('error');">Click para invocar al PluginNativo con ERROR!</button>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/HelloPlugin.js"></script>
</body>
</html>
function callNativePlugin( returnSuccess )
{
HelloPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
}
function nativePluginResultHandler (result)
{
alert("SUCCESS: \r\n"+result );
}
function nativePluginErrorHandler (error)
{
alert("ERROR: \r\n"+error );
}
var HelloPlugin =
{
callNativeFunction: function (success, fail, resultType)
{
return cordova.exec( success, fail, "HelloPlugin", "nativeAction", [resultType]);
}
};
•
•
public class CalendarPlugin extends CordovaPlugin
{
public static final String ACTION_ADD_CALENDAR_ENTRY = "addCalendarEntry";
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
throws JSONException
{
try
{
if (ACTION_ADD_CALENDAR_ENTRY.equals(action))
{
•
•
•
•
JSONObject arg_object = args.getJSONObject(0);
Intent calIntent = new Intent(Intent.ACTION_EDIT)
.setType("vnd.android.cursor.item/event")
.putExtra("beginTime", arg_object.getLong("startTimeMillis"))
.putExtra("endTime", arg_object.getLong("endTimeMillis"))
.putExtra("title", arg_object.getString("title"))
.putExtra("description", arg_object.getString("description"))
.putExtra("eventLocation", arg_object.getString("eventLocation"));
this.cordova.getActivity().startActivity(calIntent);
callbackContext.success();
return true;
}
}
callbackContext.error("Invalid action");
return false;
}
catch(Exception e)
{
System.err.println("Exception: " + e.getMessage());
callbackContext.error(e.getMessage());
return false;
}
•
<feature name="HelloPlugin">
<param name="android-package" value="com.unal.plugins.plugin.HelloPlugin" />
</feature>
<feature name="CalendarPlugin">
<param name="android-package"
value="com.unal.plugins.plugin.CalendarPlugin" />
</feature>
var calendarPlugin = {
createEvent: function(title, location, notes, startDate, endDate, successCallback, errorCallback)
{
cordova.exec(
successCallback, errorCallback, 'CalendarPlugin', 'addCalendarEntry',
}
}
);
[{"title": title,
"description": notes,
"eventLocation": location,
"startTimeMillis": startDate.getTime(),
"endTimeMillis": endDate.getTime()
}]
•
function addToCal()
{
var startDate = new Date("July 19, 2013 8:00:00");
var endDate = new Date("July 19, 2013 18:00:00");
var notes = “llegar a tiempo al evento(desde Android)";
var title = "PhoneGap Day";
var location = "Portland, OR";
var notes = “llegar a tiempo!";
var success = function() { alert(“exito"); };
var error = function(message) { alert(“error! " + message); };
calendarPlugin.createEvent(title, location, notes, startDate, endDate, success, error);
}
•
<!DOCTYPE html>
<html>
<head>
<title>Hola Plugin</title>
</head>
<body>
<h1>Prueba del nuevo Plugin!</h1>
<button onclick="callNativePlugin('success');">Click para invocar al PluginNativo con SUCCESS!</button>
<button onclick="callNativePlugin('error');">Click para invocar al PluginNativo con ERROR!</button>
<button onclick="addToCal();">Click para crear una agenda </button>
<script type="text/javascript" src="js/CalendarPlugin.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/HelloPlugin.js"></script>
</body>
</html>
•
•
•
•
public static final String ACTION_NEW_TTS_INIT = "textToSpeechInit";
public static final String ACTION_NEW_TTS_TALK = "textToSpeechTalk";
TextToSpeech ttobj;
•
@Override
public boolean execute(String action, final JSONArray args, final
CallbackContext callbackContext) throws JSONException
{
if (ACTION_NEW_TTS_INIT.equals(action))
{
cordova.getThreadPool().execute(new Runnable() {
public void run()
{
if(ttobj == null)
{
ttobj=new TextToSpeech(cordova.getActivity().getApplicationContext(),
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
ttobj.setLanguage(Locale.US);
}
}
});
}
}
});
return true;
}
else if(ACTION_NEW_TTS_TALK.equals(action))
{
cordova.getThreadPool().execute(new Runnable() {
public void run()
{
try
{
ttobj.speak(args.getString(0), TextToSpeech.QUEUE_FLUSH, null);
callbackContext.success();
}
catch(Exception e)
{
callbackContext.error(e.getMessage());
}
}
});
return true;
}
else{
return false;
}
•
<feature name="TTSPlugin">
<param name="android-package"
value="com.unal.plugins.plugin.TTSPlugin" />
</feature>
•
var TTSPlugin =
{
callTTSInitFunction: function (success, fail, resultType)
{
return cordova.exec( success, fail, "TTSPlugin", "textToSpeechInit", [resultType]);
},
};
callTTSTalkFunction: function (success, fail, resultType)
{
return cordova.exec( success, fail, "TTSPlugin", "textToSpeechTalk", [resultType]);
}
•
function callTTSInitPlugin(returnSuccess) {
TTSPlugin.callTTSInitFunction( nativePluginResultHandler, nativePluginErrorHandler,
returnSuccess );
}
function callTTSTalkPlugin( returnSuccess ) {
TTSPlugin.callTTSTalkFunction( nativePluginResultHandler, nativePluginErrorHandler,
returnSuccess );
}
function nativePluginResultHandler (result) {
alert("SUCCESS: \r\n"+result );
}
function nativePluginErrorHandler (error) {
alert("ERROR: \r\n"+error );
}
•
var app =
{
// Application Constructor
initialize: function()
{
this.bindEvents();
},
bindEvents: function()
{
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function()
{
callTTSInitPlugin('inicia');
},
};
•
•
•
•
<div id="landmark-1" onsubmit="callTTSTalkPlugin($('#title').val());">
<form>
<label for="title">TTS:</label>
<input id="title" name="title" type="text" />
<input type="submit" value="Click para TTS">
</form>
</div>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/TTSPlugin.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
app.initialize();
</script>
•
•
•
public static final String ACTION_NEW_STT_TALK = "SpeechToTextTalk";
CallbackContext callbackContext;
protected static final int RESULT_SPEECH = 1;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
throws JSONException
{
this.callbackContext = callbackContext;
if(ACTION_NEW_STT_TALK.equals(action))
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
this.cordova.getActivity().startActivityForResult(intent, RESULT_SPEECH);
return true;
}
else
{
return false;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode)
{
case RESULT_SPEECH:
if (resultCode == android.app.Activity.RESULT_OK && null != data)
{
ArrayList<String> text =
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
callbackContext.success(text.get(0));
}
else
{
//code launched in case of error
String message=data.getStringExtra("result");
callbackContext.error(message);
}
break;
default:
break;
}
}
Descargar

Integración nativa con html5