26
HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II)

HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

HTML-based Android AppsINF5750/9750 - Lecture 7 (Part II)

Page 2: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Lecture contents● Why make an HTML-based app? ● Intro to PhoneGap/Cordova● Basics of deploying an app● Most important library calls

Page 3: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Phonegap / Cordova

● Apache Cordova is a tool for packaging HTML/JS/CSS-based apps into mobile apps

● Apps can be deployed to multiple platforms● Libraries to access phone’s internal functions● Phonegap is Adobe’s distribution of Cordova● Phonegap has online build tools● Other alternatives exist: http://ionicframework.com/

Page 4: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Native vs HTML-basedNative● More battery-friendly● Uses less CPU● Faster● Can have simpler

integration with native hardware interfaces (doesn’t require plugins)

● Simpler to integrate with background services

HTML-based● Easier cross-platform

support● Same programming

language● Reuse web-components● Integrate easily with

online web service● Requires more CPU,

also depending on JavaScript framework

Page 5: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

App

HTML as an app

Server

MVC REST

Controller

HTML

JavaScript

JSON

Page 6: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

File structure

Platform specific www-files. Copied here every build.

Develop on these www-files

Android files.

Page 7: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Example Android appEmbedded web browser

Web files

Resource files, including a cordova config xml file

Page 8: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Pre-requisites● The SDK of the platform (Android, iOS,

Blackberry, Windows Phone, WebOS)● Phonegap allows you to upload and build your

project, without a local SDK (costs $$)● Node.js (A Javascript runtime)● Installing Cordova: (in theory)

○ $ sudo npm install -g cordova○ $ cordova create hello com.example.hello HelloWorld

● On Windows, to do the above, run commands inside git-bash (or similar)

Page 9: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Android SDKdeveloper.android.com/sdk

● You need Android SDK installed on your computer to create Cordova apps.

● Android SDK - ADT (Android Dev Tools)Contains APIs and libraries. Multiple versionsBuild and packaging toolsEmulator (runs the app virtually on your computer)ADB (Android Debug Bridge) - used to communicate

with emulator and real phones (over USB).

● On Windows, install the intel accelerator

Page 10: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

SDK Manager

Page 11: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

AVD Manager (in tools-menu of SDK Manager)

You can select ARM or Intel Atom as the CPU. Intel may be faster on Intel-platforms, but requires installation of some additional libraries to get optimized speed. http://software.intel.com/en-us/articles/speeding-up-the-android-emulator-on-intel-architecture

Page 12: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Command line examples$ sudo npm install -g cordova$ cordova create hello com.example.hello HelloWorld$ cd hello$ cordova platform add ios (Mac only)$ cordova platform add android$ cordova platforms ls$ cordova build$ cordova build android$ cordova emulate android$ cordova run android

Page 13: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Adding pluginsUse the command line to add plugins. These are valid for all platforms. cordova plugin add <git-address>

cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git

cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git

cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts.git

Page 14: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

config.xml● The config.xml file sets up Cordova behaviour● Specifies APIs, plugins and platform fns

<widget id="com.example.hello" version="0.0.1"> <name>HelloWorld</name> <description>A sample application</description> <author email="[email protected]" href="http://dhis2.org">Mobilars</author> <content src="index.html" /> <access origin="*" /> <preference name="Fullscreen" value="true" /> <preference name="WebViewBounce" value="true" /></widget>

Page 15: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

config.xml 2● The feature tag enabled device-level APIs<feature name="Plugin" value="PluginID" /> <feature name="App"> <param name="android-package" value="org.apache.cordova.App"/> </feature> <feature name="Geolocation"> <param name="android-package" value="org.apache.cordova.GeoBroker"/> </feature> <feature name="Device"> <param name="android-package" value="org.apache.cordova.Device"/> </feature>

Page 16: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

App pluginAccessing some basic app features.● navigator.app.clearCache● navigator.app.show● navigator.app.loadUrl('http://dhis2.org/');● navigator.app.cancelLoadUrl● navigator.app.clearHistory● navigator.app.backHistory● navigator.app.overrideButton● navigator.app.overrideBackButton● navigator.app.exitApp

Page 17: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Device pluginAccess device information (hardware and firmware) from Javascript:

var element = document.getElementById('deviceProperties'); element.innerHTML = 'Device Model: ' + device.model + '<br />' + 'Device Cordova: ' + device.cordova + '<br />' + 'Device Platform: ' + device.platform + '<br />' + 'Device UUID: ' + device.uuid + '<br />' + 'Device Version: ' + device.version + '<br />';

Page 18: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Geolocation plugin● Provides location access from Javascriptfunction onDeviceReady() {

navigator.geolocation.getCurrentPosition(onSuccess, onError); }

function onSuccess(position) { var element = document.getElementById('geolocation');

element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +

'Longitude: ' + position.coords.longitude + '<br />' +

'Altitude: ' + position.coords.altitude + '<br />' +

'Accuracy: ' + position.coords.accuracy + '<br />';

}

function onError(error) { alert('code: ' + error.code + '\n' +

'message: ' + error.message + '\n');

}

Page 19: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Passing additional geolocal parameters● You can also pass additional parametersnavigator.geolocation.getCurrentPosition(location_found, // method called when location is foundlocation_error, // method called when location !found{frequency:5000,maximumAge: 0, // Accept a cached position with age xtimeout: 10000, // ms before timing outenableHighAccuracy:true}); More info here and here. Small print: Android 2.x emulators do not return a geolocation result unless the enableHighAccuracy option is set to true.

Page 20: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Accelerometer plugin● Access accelerometer datanavigator.accelerometer.getCurrentAcceleration(onSuccess, onError);

function onSuccess(acceleration) { alert('Acceleration X: ' + acceleration.x + '\n' + 'Acceleration Y: ' + acceleration.y + '\n' + 'Acceleration Z: ' + acceleration.z + '\n' + 'Timestamp: ' + acceleration.timestamp + '\n');};function onError() { alert('onError!');};

Page 21: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Acceleration 2var options = { frequency: 3000 }; // Update every 3 seconds

var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

Page 22: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Camera plugin● Access the phone’s camera

navigator.camera.getPicture(onSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.DATA_URL});function onSuccess(imageData) { var image = document.getElementById('myImage'); image.src = "data:image/jpeg;base64," + imageData;}function onFail(message) { alert('Failed because: ' + message);}“cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git”

Page 23: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Storage plugin● Storing data on the phone

window.localStorage.setItem("key", "value");window.localStorage.setItem("key2", "value2");

var value = window.localStorage.getItem("key");

window.localStorage.removeItem("key");window.localStorage.clear();

Page 24: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Media plugin● Access the media player functions

playAudio("http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3");var my_media = null;function playAudio(src) { // Create Media object from src my_media = new Media(src, onSuccess, onError);

// Play audio my_media.play();…}

Page 25: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Other plugins● Events - pick up key presses and more● Connection - info about network status● Compass● Contacts● File● Globalization - locale/timezone● InAppBrowser● Notification● SplashScreen

Page 26: HTML-based Android Apps - Universitetet i oslo · HTML-based Android Apps INF5750/9750 - Lecture 7 (Part II) Lecture contents Why make an HTML-based app? Intro to PhoneGap/Cordova

Cordova and AngularJSTo set up a Cordova project, you can also use Yeoman (http://yeoman.io/), a scaffolding tool that helps you set up various different technology setups.

For example:https://www.npmjs.org/package/generator-angularjs-cordova(Also check out node.js and grunt)