Introduction To Android - Elite.polito.it

Transcription

Introduction toAndroidAmbient intelligenceTeodoro MontanaroPolitecnico di Torino, 2016/2017

Disclaimer This is only a fast introduction:– It is not complete (only scrapes the surface)– Only superficial notions are providedIt is a guide to self-learning and self-documentation5/10/2017Android: Fast Introduction2

Summary Short historyPlatformArchitectureAndroid app development:– Design an Android app MVC– Tools– Application fundamentals– Application Main Components part 1: Activity– 1st Hands-on: simple calculator App structure– Application Main Components part 2 Service Content Provider Broadcasts– App life cycle– Intent– 2nd Hands-on: Todo list5/10/2017Android: Fast Introduction3

History Originally created by Andy Rubin Acquired by Google Inc. in 2005 Now it is maintained by the Open Handset Alliance(OHA) (since 2007) Several stable releases since thenLast update:May 2, s/index.html)5/10/2017Android: Fast Introduction4

Figures Hundreds of millions of mobiledevices in more than 190countries around the world more than 300 hardware,software, and carrier partners Android users download morethan 1.5 billion apps and gamesfrom Google Play each month.Device activations growth5/10/2017Android: Fast Introduction5

Android Platform Android is “an open source software stack for a widerange of mobile devices and a corresponding opensource project led by Google“.1 It is composed of:– an operating system– a software platform for creating apps and games Development Tools are free:– Android applications are (mostly) written in Java programminglanguage (6 or higher)– Alternatively, a C API is available1 https://source.android.com/5/10/2017Android: Fast Introduction6

Architecture The Android operating system is a stack of softwarecomponentsSource: 017Android: Fast Introduction7

Architecture Android is based on the LinuxKernel:– takes advantage of the LinuxKernel key security features– allows device manufacturers todevelop hardware drivers for awell-known kernel5/10/2017Android: Fast Introduction8

Architecture Provides standard interfacesthat expose device hardwarecapabilities to the higher-levelJava API framework. It consists of multiple librarymodules that implementinterfaces for specific type ofhardware components (e.g.,camera, Bluetooth )5/10/2017Android: Fast Introduction9

Architecture5/10/2017 ART is an application runtimeenvironment (prior to Android5.0, Dalvik used instead ofART) It is written to run multiple virtualmachines for each runningapplication Each app runs in its ownprocess within its own instanceof the Android Runtime (ART) 10Android: Fast Introduction

Architecture Many core Android systemcomponents and services, (e.g.,ART and HAL), are built fromnative code that require nativelibraries written in C and C If you want to develop your appusing C or C , you can usethe Android NDK5/10/2017Android: Fast Introduction11

Architecture The entire feature-set of theAndroid OS is available throughJava APIs These APIs form the buildingblocks needed to createAndroid apps5/10/2017Android: Fast Introduction12

Architecture Android comes with a set ofcore apps Android doesn’t make anydistinction between native andthird-party applications5/10/2017Android: Fast Introduction13

Android app developmentDESIGN AN ANDROID APP2016/2017Android: Fast Introduction14

Design an Android app In Android 5 (lollipop) Google introduced Material Design:A new design metaphor inspired by paper and ink thatprovides a reassuring sense of tactility.Source: roduction.html5/10/2017Android: Fast Introduction15

Design an Android app Material Design is based on 3 main principles– Material is the metaphor:Build your app's components as real objects! Material is an object and realobjects are more fun than buttons and menus.– Bold, graphic, intentional:The foundational elements of print-based design – typography, grids,space, scale, color, and use of imagery – create hierarchy, meaning, andfocus.– Motion provides meaningMotion respects and reinforces the user as the prime mover; it ismeaningful and appropriate, serving to focus attention and maintaincontinuity.5/10/2017Android: Fast Introduction16

Design an Android app: MVC Best practice in developing an Android application: usethe Model View Controller (MVC) pattern5/10/2017Android: Fast Introduction17

Design an Android app: MVC Best practice in developing an Android application: usethe Model View Controller (MVC) pattern MVC is composed by 3 components:5/10/2017Android: Fast Introduction18

Design an Android app: MVC Best practice in developing an Android application: usethe Model View Controller (MVC) pattern MVC is composed by 3 components:View5/10/2017Android: Fast Introduction19

Design an Android app: MVC Best practice in developing an Android application: usethe Model View Controller (MVC) pattern MVC is composed by 3 components:View5/10/2017ModelAndroid: Fast Introduction20

Design an Android app: MVC Best practice in developing an Android application: usethe Model View Controller (MVC) pattern MVC is composed by 3 roid: Fast Introduction21

Android app developmentTOOLS2016/2017Android: Fast Introduction22

Android app development: Tools To develop an Android app we need:– Java SDK (6 or higher)– Android SDK (included in Android Studio)– Android Integrated Development Environment Android Studio (official IDE, used in this hands-on) Eclipse ADT– Android SDK emulator (included in Android Studio) Can in general be quite slow– Third-party emulators Based on hardware virtualization Typically faster E.g., GenyMotion5/10/2017Android: Fast Introduction23

Android app developmentAPPLICATION FUNDAMENTALS2016/2017Android: Fast Introduction24

Application fundamentals Android apps are written in the Java programminglanguage. Each process is run in its own virtual machine (VM), soan app's code runs in isolation from other apps. Once installed on a device, each Android app lives in itsown security sandbox:– The Android operating system is a multi-user Linux system inwhich each app is a different user.– By default, the system assigns each app a unique Linux user ID.The system sets permissions for all the files in an app so thatonly the user ID assigned to that app can access them.5/10/2017Android: Fast Introduction25

Application Main Components Each application consists of one or more of the Content providersBroadcast receivers Each of them takes care of a specific interaction insidethe Operating System– It is activated according to a specific life cycleSource: /2017Android: Fast Introduction26

Activity An activity represents a singlescreen with a user interface. An application is composed byone or more activities.– For example, an email app mighthave the following activities one that shows a list of new emails; one to compose an email; one for reading emails.5/10/2017Android: Fast Introduction27

Activity A “main” activity is mandatory in each application– it is presented to the user when she launches the application forthe first time. Each activity can start another activity– to perform different actions. Each time a new activity starts:– the previous activity is stopped,– the system preserves the activity in a stack (the "back stack"). When a new activity starts:– it is pushed onto the back stack and takes user focus5/10/2017Android: Fast Introduction28

Activity - Lifecycle As a user navigatesthrough, out of, andback to an app, theActivity instances inthis app transitthrough differentstates5/10/2017Android: Fast Introduction29

Activity - Handling lifecycle5/10/2017Android: Fast Introduction30

Activity - Handling lifecycle5/10/2017Android: Fast Introduction31

Activity - onCreate() The method onCreate(Bundle b) is called in two differentscenarios– When the activity is run for the first time The Bundle parameter is null– When the activity is launched again after being terminated (dueto lack of resources or for other reasons) The Bundle parameter contains status information This is where all normal static set up should be done:create views, bind data to lists, etc. It is always followed by onStart().5/10/2017Android: Fast Introduction32

Activity - onStart() Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to theforeground, or onStop() if it becomes hidden.5/10/2017Android: Fast Introduction33

Hands on: simple calculator5/10/2017Android: Fast Introduction39

Android Studio: Create a new app5/10/2017Android: Fast Introduction40

Android Studio: Create a new app5/10/2017Android: Fast Introduction41

App structure5/10/2017Android: Fast Introduction42

App structure The manifest filedescribes thefundamentalcharacteristics of theapp and defines eachof its components.5/10/2017Android: Fast Introduction43

App structure Among other things,the manifest containsthe followingelements:– all the singlecomponents thatcompose theapplication– the requestedpermissions– the app configurationsinformation5/10/2017Android: Fast Introduction44

App structureExample of a Manifest file5/10/2017Android: Fast Introduction45

App structure The MainActivity.javafile contains the classdefinition for the mainactivity. When the app is builtand run, the Activityclass starts theactivity and runs thecontained code It implements theController of the MVCpattern5/10/2017Android: Fast Introduction46

App structureExample: MainActivity.java5/10/2017Android: Fast Introduction47

App structure The res folder contains theresources for theapplication (it representsthe View of the MVCpattern):o drawable- density /Directories for drawableresources, other than launchericons, designed for variousdensities (e.g., drawable-hdpi)o layout/Directory for files that defineapp's user interface likeactivity main.xmlo menu/Directory for files that defineapp's menu items.5/10/2017Android: Fast Introduction48

App structureo o mipmap/Launcher icons reside in themipmap/ folder. This foldercontains the ic launcher.pngimage that appears when yourun the default app.o values/Directory for other XML files thatcontain a collection of resources,such as string (e.g., stringsdefinition for different languages)and color definitions.5/10/2017Android: Fast Introduction49

App structure: layout exampleExample of activity main.xml5/10/2017Android: Fast Introduction50

App structure: layout exampleExample of activity main.xml – Design mode5/10/2017Android: Fast Introduction51

Layout alternatives5/10/2017Android: Fast Introduction52

Layout alternatives When the content for your layout is dynamic or not predetermined, you can use one of these 2 layouts.More details at ring-layout.html5/10/2017Android: Fast Introduction53

Widgets Every widget (used to create interactive UI components(buttons, text fields, etc.)) is a subclass of the View class5/10/2017Android: Fast Introduction54

Service A service is a component that:– runs in the background to perform long-running operations or toperform work for remote processes– does not provide a user interface (e.g., play music in thebackground while the user is in a different app).– is a subclass of android.app.Service Any application component can use the service (evenfrom a separate application)– by starting it with an “Intent” (we will see it later).5/10/2017Android: Fast Introduction55

Service lifecycle5/10/2017Android: Fast Introduction56

Content provider A content provider manages a shared set of app data– Data can be stored in the file system, in an SQLite database, onthe web, or in any other persistent storage location the app canaccess, It implements a set of standard methods that allow otherapplications to fetch and to store data handled by thecurrent application– Other applications do not call its method directly, but they interactvia a content5/10/2017Android: Fast Introduction57

Broadcasts Android apps can send or receive broadcast messagesfrom the Android system and other Android apps These broadcasts are sent when an event of interestoccurs A Broadcast Receiver is a component which “waits” formessages Broadcast Receivers don't display a user interface Intended to do a very minimal amount of work (e.g.,initiate a service to perform some work)5/10/2017Android: Fast Introduction58

Other important stuff: app life-cycle A Linux process is created for an application whensome of its code needs to be run. In an ideal case– all Android processes started by the user remain inmemory Faster restart But– the available memory on an Android device is limited Therefore– the Android system is allowed to terminate running processes orrecycling Android components (lifecycle will be seen after appcomponents explanation)5/10/2017Android: Fast Introduction59

Other important stuff: app life-cycleAndroid: Processes and Application Life CycleProcess statusDescriptionPriorityForegroundA process that: a) is running an Activity at the top of the screenthat the user is interacting with, b) has a BroadcastReceiver that iscurrently running, c) has a Service that is currently executing codein one of its callbacks (Service.onCreate(), Service.onStart(), orService.onDestroy() )1VisibleUser is not interacting with the activity, but the activity is still(partially) visible. This may occur, for example, if the foregroundActivity is displayed as a dialog that allows the previous Activity tobe seen behind it.2ServiceA process that is holding a Service that has been

To develop an Android app we need: –Java SDK (6 or higher) –Android SDK (included in Android Studio) –Android Integrated Development Environment Android Studio (official IDE, used in this hands-on) Eclipse ADT –Android SDK emulator (included in Android Studio) Can in general be quite slow –Third-party emulators