Presentation is loading. Please wait.

Presentation is loading. Please wait.

Phonegap Bridge – Device,Network, Vibration,Battery,Console CIS 136 Building Mobile Apps 1.

Similar presentations


Presentation on theme: "Phonegap Bridge – Device,Network, Vibration,Battery,Console CIS 136 Building Mobile Apps 1."— Presentation transcript:

1 Phonegap Bridge – Device,Network, Vibration,Battery,Console CIS 136 Building Mobile Apps 1

2 Device 2

3 Device Plug-in org.apache.cordova.device 3  describes the device's hardware and software  Global in scope, but not available until the device is ready  Device object has 5 properties  cordova  model  platform  uuid  version

4 device.cordova 4  Gets the version of Cordova running on the device  Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.cordova); }

5 device.model 5  Gets the the name of the device's model or product  set by the device manufacturer and may be different across versions of the same product  Might get the production code name  Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.model); } Android: Nexus One returns "Passion" (Nexus One code name) Motorola Droid returns "voles" BlackBerry: Torch 9800 returns "9800" iOS: for the iPad Mini, returns iPad2,5; iPhone 5 is iPhone 5,1.

6 device.platform 6  Gets the operating system name  Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.platform); }

7 device.uuid 7  Gets the Universally Unique Identifier  a 128-bit value that is ‘practically unique’  determined by the device manufacturer and are specific to the device's platform or model.  Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.uuid); }

8 device.version 8  Gets the operating system version  Kitkat 4.4.4  Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { $(‘info’).html(device.version); }

9 Network 9

10 Network Information Plug-in org.apache.cordova.network-information 10  provides information about the device's cellular and wifi connection  Indicates if the device has an internet connection  Connection Object has 1 property and 8 constants  connection.type  Connection.UNKNOWN  Connection.ETHERNET  Connection.WIFI  Connection.CELL_2G  Connection.CELL_3G  Connection.CELL_4G  Connection.CELL  Connection.NONE

11 navigator.connection.type 11  determine the device's network connection state, and type of connection  Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { var networkState = navigator.connection.type; $(‘info’).html(networkState); }

12 Network States 12  Using the type of connection, coupled with the translation of network state constants, can provide textual description - quirky document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { var networkState = navigator.connection.type; var states = {}; states[Connection.UNKNOWN] = 'Unknown connection'; states[Connection.ETHERNET] = 'Ethernet connection'; states[Connection.WIFI] = 'WiFi connection'; states[Connection.CELL_2G] = 'Cell 2G connection'; states[Connection.CELL_3G] = 'Cell 3G connection'; states[Connection.CELL_4G] = 'Cell 4G connection'; states[Connection.CELL] = 'Cell generic connection'; states[Connection.NONE] = 'No network connection'; $(‘info’).html(states[networkState]); }

13 Network related events 13  offline - fires when an application goes offline, and the device is not connected to the Internet document.addEventListener("offline", yourCallbackFunction, false);  online - fires when an application goes online, and the device becomes connected to the Internet document.addEventListener("online", yourCallbackFunction, false);

14 Vibration 14

15 Vibration 15  navigator.notification object  Method:  vibrate(duration)  duration is specified in milliseconds  To make a device vibrate, pause, then vibrate again, need to call vibrate several times and force the app to pause in between  navigator.notification.vibration(2000);

16 Battery 16

17 Battery Status Plug-in org.apache.cordova.battery-status 17  a means for web developers to programmatically determine the battery status of the hosting device and whether the device is plugged in or not  without knowing the battery status of a device, you are designing the web application with the assumption there is sufficient battery level for the task at hand  battery of a device may exhaust faster than expected if apps are unable to make decisions based on the battery status  given knowledge of the battery status, web developers are able to craft web content and applications which are power- efficient, thereby leading to improved user experience

18 Battery Status Plug-in 18  Battery Status events can be used to defer or scale back work when the device is not plugged in or is low on battery  Ex:  In an advanced application, like an email app, the app can check the server for new email every few seconds if the device is plugged in, but do so less frequently if the device is not plugged in or is low on battery  In an advanced application, like a note keeper, the app could monitor the battery level and save changes before the battery runs out to prevent data loss

19 Battery Status Attributes 19  isPlugged (Boolean)  whether the device is plugged in  Level (float)  Represents how much of the internal power source remains, scaled from 0 to 100 (null if no juice)  Status (string)  the battery status of the hosting device, scaled from critical to ok

20 Battery Status Attributes 20  Critical  Low  OK  null - unable to report the battery's level

21 Battery Status Event 21  BatteryStatus event is triggered when  isPlugged changes its value, or  level changes by at least 1, or  status changes its value  This event fires when the percentage of battery charge changes by at least 1 percent, or if the device is plugged in or unplugged

22 Battery Status Example 22 window.addEventListener("batterystatus", onBatteryStatus, false); function onBatteryStatus(info) console.log("Level: " + info.level + " isPlugged: " + info.isPlugged); } o The battery status handler is passed an object that contains two properties: o level: The percentage of battery charge (0-100) o isPlugged: A boolean that indicates whether the device is plugged in o use an EventListener to attach an event handler after the deviceready event fires

23 Battery Critical 23  The event fires when the percentage of battery charge has reached the critical battery threshold  The value is device-specific

24 Battery Critical Example 24  The batterycritical handler is passed an object that contains two properties:  level: The percentage of battery charge (0-100)  isPlugged: A boolean that indicates whether the device is plugged in window.addEventListener("batterycritical", onBatteryCritical, false); function onBatteryCritical(info) { alert("Battery Level Critical " + info.level + "%\nRecharge Soon!"); }

25 Battery Low 25  This event fires when the percentage of battery charge has reached the low battery threshold  device-specific value

26 Battery Low Example 26  The batterylow handler is passed an object that contains two properties:  level: The percentage of battery charge (0-100)  isPlugged: A boolean that indicates whether the device is plugged in window.addEventListener("batterylow", onBatteryLow, false); function onBatteryLow(info) { alert("Battery Level Low " + info.level + "%"); }

27 Console 27

28 Cordova Console Plugin org.apache.cordova.console 28  ensure that console.log() is as useful as it can be  Ex: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { console.log(“message to console – Hello there”); }


Download ppt "Phonegap Bridge – Device,Network, Vibration,Battery,Console CIS 136 Building Mobile Apps 1."

Similar presentations


Ads by Google