Post on 19-May-2015
description
Android Plugins
Agenda
What makes me qualified to talk about this? What is PhoneGap? How do I extend PhoneGap? Questions? Let's Hack!
What makes me qualified to talk about this?
Currently top committer to PhoneGap-Android
Authored Various Plugins
BarcodeScanner – scans and encodes bar codes.
ChildBrowser – open remote web pages without interrupting your app.
FTPClient – pretty obvious TTS – Text to Speech service VideoPlayer – works around broken <video/>
tag in WebView
What is PhoneGap?
PhoneGap is...
a tool for building mobile applications using web technologies.
HTML for layout JavaScript to access device functionality CSS for rich look and feel Standards based
How do I extend PhoneGap?
Plugins to the rescue
Clean from an engineering perspective.
Lighter builds possible.
Adaptable for 3rd party extensions.
Portable to new platforms.
Secure only use what you need.
Plugin Native Interface
Plugin JavaScript Interface
Basic Example
Create you JavaScript
Basic Example Create you Java Code
Basic Example
Add plugin to config file res/xml/plugins.xml
<plugin name="HelloWorld" value="com.phonegap.plugins.hello.HelloWorld"/>
Call it from JavaScript:
window.plugins.helloWorld.sayHi('Hi Android');
Callbacks In your PhoneGap.exec() the first two parameters are the
success and failure callbacks.
The success call back is executed when your Java code returns a status of PluginResult.Status.OK
The failure call back is executed when you return: PluginResult.Status.CLASS_NOT_FOUND_EXCEPTION PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION PluginResult.Status.INSTANTIATION_EXCEPTION PluginResult.Status.MALFORMED_URL_EXCEPTION PluginResult.Status.IO_EXCEPTION PluginResult.Status.INVALID_ACTION PluginResult.Status.JSON_EXCEPTION PluginResult.Status.ERROR
Keep Alive But what if you want to call your success call
back multiple times. Save the callback IDthis.callbackId = callbackId;
Use the keepAlive parameter.PluginResult result = new
PluginResult(PluginResult.Status.NO_RESULT);result.setKeepCallback(true);
Call the success function whenever you have a new result:
PluginResult result = new PluginResult(PluginResult.Status.OK, info);
result.setKeepCallback(true);
this.success(result, this.callbackId);
Web Intent Plugin Sometimes it isn't necessary to create a brand
new plugin if you just want to call an intent. The solution is Boris Smus' WebIntent Plugin.window.plugins.webintent.startActivity(extras, success, fail);
Send an email for instance:window.plugins.webintent.startActivity({
action: WebIntent.ACTION_SEND,
type: 'text/plain',
extras: extras
}, function() {},
function() {
alert('Failed to send email via Android Intent');
}
);
Questions?
Let's Hack!
Resources
More Info – http://www.phonegap.com/ Getting Started – http://www.phonegap.com/start Mailing List / Google Group –
http://groups.google.com/group/phonegap API Documentation – http://docs.phonegap.com/ Wiki –
http://wiki.phonegap.com/w/page/16494772/FrontPage CODE – http://github.com/phonegap