Plugins
While the PhoneGap API has an increasing number of hooks into native phone functions, not all of the functions you might want to access are available through the PhoneGap API yet. To fill that gap, the architects of PhoneGap have created a plugin system that allows PhoneGap to be extended to include any native phone functions you want.
Plugins can be found in many places in the web including here - https://github.com/phonegap/phonegap-plugins
To use a new plugin that you find to you Android PhoneGap project, you need to do the following steps:
- Copy the plugin.js file to your assets/www directory
- Add a reference to the js file in your html
e.g.
<Script src="smsplugin.js" type="text/javascript" ></script>
- Copy the directory tree including the plugin.java file
- Add the plugin to res/plugins/plugins.xml
e.g. <plugin name="SmsPlugin" value="net.practicaldeveloper.phonegap.plugins.SmsPlugin"/>
- Add any necessary permissions to your AndroidManifest.xml
e.g. <uses-permission android:name="android.permission.SEND_SMS"/>
Once you have the .js, .java and permissions set up correctly you can try out the new plugin. For the SMS plugin, you can add this to your javascript:
// sends an sms
function sendSMS() {
console.log('sendSMS()');
smsNumber = document.getElementById("smsNumber").value;
smsMessage = document.getElementById("smsMessage").value;
console.log('SMS attempt: ' + smsNumber + ', message=' + smsMessage);
window.plugins.sms.send(smsNumber, smsMessage, function() {
alert('SMS sent to: ' + smsNumber + ', message=' + smsMessage); },
function(e) { alert('Message Failed:' + e); }
);
}
And add this to your HTML:
<input id="smsMessage" type="text" value="Enter Message"
style="width: 200px" />
<input id="smsNumber" type="text" value="Enter Number"
style="width: 200px" />
<button onclick="sendSMS();">Send SMS</button>
Et voila, you can now send SMS from within your Android PhoneGap app.
Now what if you want to send SMS from your app on iPhone as well as Android?
At that point, you would need to find or write an SMS pluging for iPhone, e.g. https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/SMSComposer and call the appropriate plugin depending on which device platform your app is running - http://docs.phonegap.com/en/1.4.1/phonegap_device_device.md.html#device.platform