Practical No.1: Cordova Basics and Configurations

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Practical No.1: Cordova Basics and Configurations.

Installation: We need the following softwares.


a) Node.js
b) Git
c) Java (jdk 8.1)
d) Android SDK Manager(or SDK Manager or Studio) with Emulator(AVD)
Install Node.js

P age |1 SYBSc(IT) SEM – III MOBILE PROGRAMMING


 Install Git

P age |2 SYBSc(IT) SEM – III MOBILE PROGRAMMING


 Install Cordova
Run command in command prompt: npm install –g cordova

P age |3 SYBSc(IT) SEM – III MOBILE PROGRAMMING


 Install Java

P age |4 SYBSc(IT) SEM – III MOBILE PROGRAMMING


 Install Android Studio

P age |5 SYBSc(IT) SEM – III MOBILE PROGRAMMING


For checking the versions of the Installed software’s:

Add the path in


Properties – Advanced system settings – Environment variables
In User Variable

P age |6 SYBSc(IT) SEM – III MOBILE PROGRAMMING


In System variables

P age |7 SYBSc(IT) SEM – III MOBILE PROGRAMMING


P age |8 SYBSc(IT) SEM – III MOBILE PROGRAMMING
Practical no.2: Creating and Building Simple “Hello World” App using Cordova
 Step 1: Creating a new directory and project.

 Step 2: Adding the Platform for application.

Index.html
<body>
<div class="app">

<h1>Hello World</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
</body>

 Step 3: Build the Project.

P age |9 SYBSc(IT) SEM – III MOBILE PROGRAMMING


 cordova emulate android

P a g e | 10 SYBSc(IT) SEM – III MOBILE PROGRAMMING


Practical No. : 3 Configuration of Cordova Project
Once we have created a cordova project a directory structure is created
on machine is as below

Here we get a file named config.xml

The file contains all the configuration elements related to our project. We can also change the
configuration here in this file as per our requirement.

The file contains code below

Config.xml
<?xml version='1.0' encoding='utf-8'?>

<widget id="io.cordova.hello" version="1.0.0"xmlns="http://www.w3.org/ns/widgets"


xmlns:cdv="http://cordova.apache.org/ns/1.0">

<name>HelloApp</name>

<description>

A sample Apache Cordova application that responds to the deviceready event.

</description>

<author email="[email protected]" href="http://cordova.io">

Apache Cordova Team

</author>

<content src="index.html" />

<plugin name="cordova-plugin-whitelist" spec="1" />

<access origin="*" />

<allow-intent href="http://*/*" />

<allow-intent href="https://*/*" />


P a g e | 11 SYBSc(IT) SEM – III MOBILE PROGRAMMING
<allow-intent href="tel:*" />

<allow-intent href="sms:*" />

<allow-intent href="mailto:*" />

<allow-intent href="geo:*" />

<platform name="android">

<allow-intent href="market:*" />

</platform>

<platform name="ios">

<allow-intent href="itms:*" />

<allow-intent href="itms-apps:*" />

</platform>

</widget>

P a g e | 12 SYBSc(IT) SEM – III MOBILE PROGRAMMING


Practical no.4: Adding and using Event Listeners with Functions.
 Creating new application,

 Open the index.js file.


receivedEvent: function(id)
{

document.addEventListener("volumeupbutton",volUpFunction, false);
document.addEventListener("volumedownbutton",volDownFunction, false);;

var parentElement = document.getElementById(id);


var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');

listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');

console.log('Received Event: ' + id);


}
};

function volUpFunction()
{

P a g e | 13 SYBSc(IT) SEM – III MOBILE PROGRAMMING


alert('I am Volume Up Button...')
};
function volDownFunction()
{
alert('I am Volume Down Button...')
};
app.initialize();

 Now build and run the application.

Path of Apk
D:\demo\EveFun\platforms\android\app\build\outputs\apk\debug

P a g e | 14 SYBSc(IT) SEM – III MOBILE PROGRAMMING


P a g e | 15 SYBSc(IT) SEM – III MOBILE PROGRAMMING
Practical No.5: Adding Using Buttons.
 Create new Cordova Project for button demo.

 Add the android platform .

 Open index.html
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<button id = "HelloWorld">Click Me</button>
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>

 Open index.js

onDeviceReady: function()
{
document.getElementById("HelloWorld").addEventListener("click",
sayHello);
this.receivedEvent('deviceready');
},
function sayHello()
{
alert('Welcome to The World Of Mobile Programming......');
};
app.initialize();

P a g e | 16 SYBSc(IT) SEM – III MOBILE PROGRAMMING


 Build the application.

 Run the Project.


Path of Apk

D:\demo\ButtonCP\platforms\android\app\build\outputs\apk\debug

P a g e | 17 SYBSc(IT) SEM – III MOBILE PROGRAMMING


Practical No.6: Handling and Using Back Button.
 Create cordova for back button.

 Create android platform.

 Open index.js file.


document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e)
{
e.preventDefault();
alert('I am Back Button');
}

 Build and Run the application.

P a g e | 18 SYBSc(IT) SEM – III MOBILE PROGRAMMING


P a g e | 19 SYBSc(IT) SEM – III MOBILE PROGRAMMING
P a g e | 20 SYBSc(IT) SEM – III MOBILE PROGRAMMING
Practical No.7: Installing and Using Plugins.

Run Command in command Prompt:


npm install –g plugman

 Installing Plugman.

 Installing Plugins.

P a g e | 21 SYBSc(IT) SEM – III MOBILE PROGRAMMING


Practical No.8 : Installing and Using Battery Status Plugins.
 Creating new application.

 Adding battery status plugin.

 Open the index.js file.


receivedEvent: function(id) {
window.addEventListener("batterystatus", onBatteryStatus, false);
window.addEventListener("batterylow", onBatteryLow, false);
window.addEventListener("batterycritical", onBatteryCritical, false);
}
function onBatteryStatus(info){
alert("BATTERY STATUS:\n Whats the Level?" + info.level +"\n is Plugged?"
+ info.isPlugged);
};
function onBatteryLow(status){
alert("Battery is in Low Level" + status.level + "%");
};
function onBatteryCritical(status){
P a g e | 22 SYBSc(IT) SEM – III MOBILE PROGRAMMING
alert("Battery is Critical Now" + status.level + "%\n Start charging Soon!");
};
app.initialize();

 Build and Run the application.

P a g e | 23 SYBSc(IT) SEM – III MOBILE PROGRAMMING


P a g e | 24 SYBSc(IT) SEM – III MOBILE PROGRAMMING
Practical No.9: Installing and Using Camera Plugin.
 Creating new application.

 Adding camera plugin.

 Open index.html file.


<div class="app">
<button id = "takePic">TAKE PICTURE</button>
<img id = "myImg"></img>
</div>

 Open index.js file.


receivedEvent: function(id)
{
document.getElementById("takePic").addEventListener("click",takePic);
}
And before the app.initialize(); write the function shown below.,
function takePic()
{
navigator.camera.getPicture(onSuccess,onFail,
{
quality:50,
destinationType:Camera.DestinationType.DATA_URL
});
function onSuccess(imageData){
var image = document.getElementById('myImg');
image.src = "data:image/jpeg;base64,"+imageData;
};
P a g e | 25 SYBSc(IT) SEM – III MOBILE PROGRAMMING
function onFail(message)
{
alert('Failed because:'+message);
};
};
app.initialize();

 Build and Run the application.

P a g e | 26 SYBSc(IT) SEM – III MOBILE PROGRAMMING


P a g e | 27 SYBSc(IT) SEM – III MOBILE PROGRAMMING
Practical No.10: Installing and Using Contacts Plugin.
 Creating new application.

 Adding contact plugin.

 Open index.html file.


<div class="app">
<h3>Handling Contacts</h3>
<button id="addContact">ADD CONTACT</button><br><br>
<button id="searchContact">FIND CONTACT</button>
</div>

 Open index.js file.


receivedEvent: function(id)
{
document.getElementById("addContact").
addEventListener("click",addContact);
document.getElementById("searchContact").
addEventListener("click",searchContact);
}

function addContact()
{
var contact=navigator.contacts.create();
contact.displayName="Tushar";
contact.nickname="Tushar";
contact.name="Tushar";
P a g e | 28 SYBSc(IT) SEM – III MOBILE PROGRAMMING
contact.save(contactSuccess,contactError);
function contactSuccess
{alert("Contact saved!")
}
function contactError(message)
{alert('Failed because:' +message);
}
};

function searchContact()
{
var myContact=navigator.contacts.create({"displayName":"Test User"});
var options = new ContactFindOptions();
options.filter="Tushar";
options.multiple=true;
options.desiredFields=[navigator.contacts.fieldType.id];
options.hasPhoneNumber=false;
var fields=[navigator.contacts.fieldType.displayName,navigator.contacts.
fieldType.name];
navigator.contacts.find(fields,onSuccess,onError,options);

function onSuccess(contacts)
{alert('Found' +contacts.length+'contacts.');
};
function onError(contactError)
{alert('onError!');
};
};
app.initialize();

 Build and Run the application.

P a g e | 29 SYBSc(IT) SEM – III MOBILE PROGRAMMING


P a g e | 30 SYBSc(IT) SEM – III MOBILE PROGRAMMING
Practical No.11: Installing and Using Device Plugin.
 Creating new application.

 Adding device plugin.

 Open index.html file.


<div class="app">
<h3>Get The Device Details</h3>
<button id="myDevice">KNOW DEVICE DETAILS</button>
</div>

P a g e | 31 SYBSc(IT) SEM – III MOBILE PROGRAMMING


 Open index.js file.
receivedEvent: function(id)
{
document.getElementById("myDevice")
.addEventListener("click", myDeviceDetails);
}
function myDeviceDetails() {
alert("My Cordova version: " + device.cordova + "\n" +
"My Device model: " + device.model + "\n" +
"My Device platform: " + device.platform + "\n" +
"My Device UUID: " + device.uuid + "\n" +
"My Device version: " + device.version);
};
app.initialize();

 Build and Run the application.

P a g e | 32 SYBSc(IT) SEM – III MOBILE PROGRAMMING


P a g e | 33 SYBSc(IT) SEM – III MOBILE PROGRAMMING
P a g e | 34 SYBSc(IT) SEM – III MOBILE PROGRAMMING

You might also like