Phone gap android plugins

21
Android Plugins

description

Ottawa Android presentation Oct 8th.

Transcript of Phone gap android plugins

Page 1: Phone gap android plugins

Android Plugins

Page 2: Phone gap android plugins

Agenda

What makes me qualified to talk about this? What is PhoneGap? How do I extend PhoneGap? Questions? Let's Hack!

Page 3: Phone gap android plugins

What makes me qualified to talk about this?

Page 4: Phone gap android plugins

Currently top committer to PhoneGap-Android

Page 5: Phone gap android plugins

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

Page 6: Phone gap android plugins

What is PhoneGap?

Page 7: Phone gap android plugins

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

Page 8: Phone gap android plugins
Page 9: Phone gap android plugins

How do I extend PhoneGap?

Page 10: Phone gap android plugins

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.

Page 11: Phone gap android plugins

Plugin Native Interface

Page 12: Phone gap android plugins

Plugin JavaScript Interface

Page 13: Phone gap android plugins

Basic Example

Create you JavaScript

Page 14: Phone gap android plugins

Basic Example Create you Java Code

Page 15: Phone gap android plugins

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');

Page 16: Phone gap android plugins

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

Page 17: Phone gap android plugins

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);

Page 18: Phone gap android plugins

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');

}

);

Page 19: Phone gap android plugins

Questions?

Page 20: Phone gap android plugins

Let's Hack!

Page 21: Phone gap android plugins

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