Introduction
Hello everyone, this is the second article about Windows Phone 8 app development using PhoneGap (Cordova).
In the last article we talked about the PhoneGap (Cordova) installation process in Visual Studio 2013 and how to create our first “Hello world” Windows Phone project using Visual Studio and PhoneGap (Cordova).
At the end we saw two ways to integrate jQuery mobile with our Visual Studio project to make our project more beautiful and professional.
Link of first article: Windows Phone 8 App Development Using PhoneGap (Cordova): Part 1.
Topics will be covered
Here in this article we will see various PhoneGap APIs and code to use various sensors or capabilities of a Windows Phone device. In this article, we will cover topics like:
- Popup Window
- Vibrate
- Connection Type
- Device Info
- Camera Capture
- Back Button
- Accelerometer
- Com
- Geo-location
- Battery Status
- Collapsible Control
- Panel Control
Interface of the Application
The following images are the screen shots of the long list of selectors and using that we will select various functionalities and actions.
Here, in the preceding image we can see a long list of selectors. Using this long list of selectors we will see various actions of the available functionalities of PhoneGap in Windows Phone.
Code Behind
The code from this long list selector is given below. Each section is described separately and briefly.
jQuery Mobile Reference
Since we are using jQuery Mobile UI, we added these three lines of reference code first. This is the reference code of the jQuery Mobile Theme.
- <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
- <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
- <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
Listing 1
Using Long List Selector
The HTML code of this long list selector is given below.
- <div data-role="page" id="pageone">
- <div data-role="header">
- <h1>Phonegap</h1>
- </div>
- <ul data-role="listview" data-inset="true">
- <li><a href="#pagetwo">Show Pop Up</a></li>
- <li><a href="#pagethree">Vibrate</a></li>
- <li><a href="#pagefour">Connection Type</a></li>
- <li><a href="#pagefive">Device Info</a></li>
- <li><a href="#pagesix">Camera Capture</a></li>
- <li><a href="#pageseven">Back Button</a></li>
- <li><a href="#pageeight">Accelerometer</a></li>
- <li><a href="#pagenine">Com</a></li>
- <li><a href="#pageten">GeoLocation</a></li>
- <li><a href="#pagetwelve">Battery Status</a></li>
- <li><a href="#pageeleven">collapsible Control</a></li>
- <li><a href="#pagethirteen">Panel Control</a></li>
-
- </ul>
-
- <div data-role="footer">
- <h1>APIs</h1>
- </div>
- </div>
- ...
- </div>
Listing 2
Popup Control
Now if we select the first option in the long list selector, a Pop-up, then we will see a screen as in the following page. Then, if we click on the Click Me button, then a Pop-up menu will appear with the text of Button Clicked.
HTML and JavaScript code of Pop-up menu.
- <div data-role="page" id="pagetwo">
- <div data-role="header">
- <h1>Welcome To My Homepage</h1>
- </div>
-
- <div data-role="main" class="ui-content">
- <a href="#" onclick="btn_click()" class="ui-btn">Click Me</a>
- <a href="#pageone">Go back to Home page</a>
- </div>
- <div data-role="footer">
- <h1>Footer Text</h1>
- </div>
- </div>
- function btn_click()
- {
- navigator.notification.alert("Button Clicked",null,"Pop Up","");
- }
Listing 3
Using Vibrate
Now we will look at the mobile device Vibrate API. The following is the screen shot of the vibrate me page.
Figure 3
We can vibrate our mobile device by calling the following JavaScript vibrate function. The single line of JavaScript code is initializing the vibration in the device and here the duration of vibration is 1 second (1000 miliseconds).
- function vibrate()
- {
- navigator.notification.vibrate(1000)
- }
Listing 4
Connection Type Figure 4
We will now see the connection type of our mobile device. We can get the connection type of the device by calling the following JavaScript Connection_Type function. Here we are declaring a variable named networkState and then assigning the connection type value to this variable by calling the navigation.network.connection.type code. Then we can see the network status using an alert message.
- function Connection_Type()
- {
- var networkState = navigator.network.connection.type;
- navigator.notification.alert(networkState);
- }
Listing 5
Device Information Figure 5
Here we can see the Device info of our mobile device by calling the JavaScript Device_Info function. Here, the JavaScript APIs like, device.name, device.platform, device.uuid, device.version are showing us the Device Name, platform, Unique ID and Version of the mobile device. Then, an alert message is showing us the device info.
function Device_Info()
- {
- var DeviceName = device.name;
- var Platform = device.platform;
- var UniqueID = device.uuid;
- var Version = device.version;
- navigator.notification.alert('Device Name : ' + DeviceName + '\n Platform : ' + Platform
- + '\n UUID : ' + UniqueID + '\n Version : ' + Version);
- }
Listing 6
Camera CaptureWe can take a picture, save it and preview it in Windows Phone using the JavaScript API. Here, we are calling the Camera Capture function and this function is invoking the camera and then we can take a picture. We are using camera.getPicture and if our application successfully invokes the camera and takes the picture then it will move to the onSuccess function and then takes an image control and shows the recently taken photo on that image control by setting its source.
- function Camera_Capture() {
- navigator.camera.getPicture(onSuccess, onFail, { sourceType: Camera.PictureSourceType.CAMERA });
- }
-
- function onSuccess(data) {
- var imageControl = document.getElementById('image');
- imageControl.src = "data:image/jpeg;base64," + data;
- }
-
- function onFail(message) {
- alert('Error taking picture');
- }
Listing 7
Back Button Figure 7
You can simply use the “Back Button” control by adding a single line of code. Declare the data-rel="back", in the Button property. Here is the code.
- <div data-role="page" id="pageseven">
- <div data-role="header">
- <h1>Welcome To My Homepage</h1>
- </div>
-
- <div data-role="main" class="ui-content">
- <a href="#" class="ui-btn" data-rel="back">Go Back</a>
- <a href="#pageone">Go back to Home page</a>
- </div>
- <div data-role="footer">
- <h1>Footer Text</h1>
- </div>
- </div>
Listing 8
Accelerometer Figure 8
Here we can see the Acceleration on the X, Y, Z axes and timestamp using the Accelerometer sensor of the Windows Phone device. Here, the JavaScript APIs like acceleration.x, acceleration.y, acceleration.z and acceleration.timestamp are showing us the X,Y,Z acceleration and timestamp of the mobile device. Then, an alert message is showing us the acceleration info.
- function Dev_Ready()
- {
- navigator.accelerometer.getCurrentAcceleration(onSuccess, Error);
- }
-
- function onSuccess(acceleration)
- {
- alert('Acceleration X: ' + acceleration.x + '\n' +
- 'Acceleration Y: ' + acceleration.y + '\n' +
- 'Acceleration Z: ' + acceleration.z + '\n' +
- 'Timestamp: ' + acceleration.timestamp + '\n');
- }
-
- function Error()
- {
- alert('onError!');
- }
Listing 9
Com Figure 9
Here we are calling com.getCurrentHeading to get the Com heading of the mobile device then if our application successfully calls the getCurrentHeading then then the application will show the current heading value using heading.magneticHeading using a message box.
- function Com()
- {
- navigator.com.getCurrentHeading(Success, Errror);
- }
-
- function Success(heading)
- {
- alert('Heading: ' + heading.magneticHeading);
- }
-
- function Errror(comError)
- {
- alert('Com Error: ' + comError.code);
- }
Listing 10
Geolocation Figure 10
The following is the JavaScript code to get the current location of the mobile device. At first we are calling geolocation.getCurrentPosition and if it is successfully called then we will call the GeoSuccess function and the data of the position that we get from getCurrentPosition. Then, by using position.coords.lattitude and position.coords.longitude we will get the longitude and latitude value of the current location and show it using a message box.
- function Geo_Location()
- {
- navigator.geolocation.getCurrentPosition(GeoSuccess, GeoError);
- }
-
- function GeoSuccess(position)
- {
- var element = document.getElementById('geolocation');
- alert('Latitude: ' + position.coords.latitude + '\n' +
- 'Longitude: ' + position.coords.longitude + '\n' +
- 'Altitude: ' + position.coords.altitude + '\n' +
- 'Accuracy: ' + position.coords.accuracy + '\n' +
- 'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
- 'Heading: ' + position.coords.heading + '\n' +
- 'Speed: ' + position.coords.speed + '\n' +
- 'Timestamp: ' + position.timestamp);
- }
-
- function GeoError(error)
- {
- alert('code: ' + error.code + '\n' +
- 'message: ' + error.message + '\n');
- }
Listing 11
Battery Status
The following is the code to get the battery status of the device. We can see the battery status if the current battery level increases or decreases. If the current battery level is the same then the message will not show. We can see the battery status only if the status changes.
- window.addEventListener("batterystatus", onBatteryStatus, false);
- function onBatteryStatus(info)
- {
-
-
- alert("Level: " + info.level + "%, isPlugged: " + info.isPlugged);
- }
Listing 12
Collapsible ControlThe following is the code of the collapsible control in the jQuery mobile.
- <div data-role="page" id="pageeleven">
- <div data-role="header">
- <h1>Insert Page Title Here</h1>
- </div>
-
- <div data-role="main" class="ui-content">
- <div data-role="collapsible" data-theme="b" data-content-theme="b">
- <h1>Click me - I'm collapsible!</h1>
- <p>I'm the expanded content.</p>
- </div>
- </div>
-
- <div data-role="footer">
- <h1>Insert Footer Text Here</h1>
- </div>
- </div>
Listing 13
Panel ControlThis is the code of the opening of the panel in a page. If we click on the button then a different panel will appear from the right side of the application:
- <div data-role="page" id="pagethirteen">
- <div data-role="panel" id="myPanel" data-theme="b">
- <h2>Panel Header</h2>
- <p>I am a themed panel!</p>
- <p> You can close me by clicking outside of me, pressing the Esc key or by swiping.</p>
- </div>
-
- <div data-role="header">
- <h1>Page Header</h1>
- </div>
-
- <div data-role="main" class="ui-content">
- <p>Click on the button below to open the themed Panel.</p>
- <a href="#myPanel" class="ui-btn ui-btn-b ui-btn-inline ui-corner-all ui-shadow">Open Panel</a>
- </div>
-
- <div data-role="footer">
- <h1>Page Footer</h1>
- </div>
- </div>
Listing 14
Summary
I hope you've understoond how to use these controls in PhoneGap. For further reading just visit the following links. Have a nice day. Happy Coding!
PhoneGap Documentation of Windows Phone APIs:
Getting Started with Windows Phone 8.jQuery Mobile Control Codes:
jQuery Mobile Tutorial << Windows Phone 8 App Development Using PhoneGap (Cordova): Part 1.