27
CS260 Intro to Java & Android 04.Android Intro Winter 2020 Winter 2020 CS260 - Intro to Java & Android 1

CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

CS260 Intro to Java & Android04.Android Intro

Winter 2020

Winter 2020 CS260 - Intro to Java & Android 1

Page 2: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Android - Getting Started

Android SDK contains:

API Libraries

Developer Tools

Documentation

Sample Code

Present development tools:

IntelliJ IDEA

Android Studio

Winter 2020 CS260 - Intro to Java & Android 2

Page 3: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Android Portability

ART (Android Runtime)

- replaces Dalvik since in 4.4

- translates app's bytecode into native instructions

Development Platforms:

Windows (Windows 7, 8, 10)

Linux

Mac OS 10.10 - 10.13 or later (Intel chips only)

Winter 2020 CS260 - Intro to Java & Android 3

Page 4: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Android HelloWorld Application

Winter 2020 CS260 - Intro to Java & Android 4

• Figure 04.1

Page 5: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

New HelloWorld Project

• Figure 04.2

• Empty Activity

Winter 2020 CS260 - Intro to Java & Android 5

Page 6: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

New HelloWorld Project

• Figure 04.3

Winter 2020 CS260 - Intro to Java & Android 6

Page 7: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Development Environment

Figure 04.4

Winter 2020 CS260 - Intro to Java & Android 7

Page 8: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Development Environment

Figure 04.5 ctl+shift+alt+S

Winter 2020 CS260 - Intro to Java & Android 8

Page 9: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Development EnvironmentFigure 04.6

Winter 2020 CS260 - Intro to Java & Android 9

Page 10: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

activity_main.xmlFigure 04.7

Winter 2020 CS260 - Intro to Java & Android 10

Page 11: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Creating Virtual Devices

Winter 2020 CS260 - Intro to Java & Android 11

• Tools ->AVD Manager (Create Nexus 4 Android 5.1) then start device

• Figure 04.8

Page 12: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

What is Android?

Android is a software stack (set of programs working together) for mobile devices that includes:

an operating system

middleware

applications

Winter 2020 CS260 - Intro to Java & Android 12

Page 13: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Application Fundamentals

Android apps can be written in Kotlin, Java, C++

Kotlin reference https://kotlinlang.org/docs/reference/faq.html

SDK tools compile code into APK (Android application package … e.g. helloandroid.apk)

One .apk file contains all app contents

Winter 2020 CS260 - Intro to Java & Android 13

Page 14: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Android Architecture

Figure 04.9

Winter 2020 CS260 - Intro to Java & Android 14

Page 15: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Linux Kernel

Android relies on Linux for:

memory management

process management

security

networking

You will not make Linux system calls

Some utilities interact with Linux

e.g. adb shell

Winter 2020 CS260 - Intro to Java & Android 15

Page 16: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Application Framework

Android developers have access to the same framework APIs use by the core applications

Services and systems for applications include:

Views – including lists, grids, buttons, ....

Content Providers – methods for accessing data

Resource Manager – organizes non-code resources such as strings and layout files

Notification Manager – displays custom alerts

Activity Manager – manages lifecycle of applications

Winter 2020 CS260 - Intro to Java & Android 16

Page 17: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Android Runtime

Every Application:

Runs in its own process space

Has a separate instance of the Android Runtime (ART)

ART uses the Linux kernel for functionality such as threading and low-level memory management

Winter 2020 CS260 - Intro to Java & Android 17

Page 18: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Features of ART?

Executes DEX files (special bytecode format)

Ahead-of-time (AOT) and just-in-time (JIT) compilation

Optimized garbage collection

Winter 2020 CS260 - Intro to Java & Android 18

Page 19: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Android Applications

Apps are written in Java

Code is compiled into Android package (.apk file)

All code (including data & resource files) in .apkis one application

Winter 2020 CS260 - Intro to Java & Android 19

Page 20: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Big Picture

Figure 04.11

Winter 2020 CS260 - Intro to Java & Android 20

Page 21: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Android Application Specifics

Android is a multi-user Linux system where each application is a user

Only one application is visible at a time

Each process has its own VM running an application in isolation

Two or more applications can share data

Applications consist of one or more activities

Winter 2020 CS260 - Intro to Java & Android 21

Page 22: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

What is an Activity?

An Activity represents a single screen with a UI

Ex: Email Application consists of activities for

Showing list of emails

Composing an email

Reading an email

Each activity is independent

Other applications can use a particular activity if the email application gives permission to do so

Winter 2020 CS260 - Intro to Java & Android 22

Page 23: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Activity Lifecycle

Activity – a process that performs some specific action

Every Android application is made up of one or more activities managed on an Activity Stack (AS) or the “back stack”.

A new activity is always placed on top of the AS and then becomes the running activity.

The AS is LIFO; therefore, when the Back button is pressed the current activity is popped and destroyed

Winter 2020 CS260 - Intro to Java & Android 23

Page 24: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Activity Lifecycle VisualFigure 04.12

Winter 2020 CS260 - Intro to Java & Android 24

Page 25: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Activity States

An activity has essentially four states:

running – in the foreground of the screen

paused – lost focus but still visible with all state maintained

How? A new activity that is transparent or not full sized is running on top of the stack

stopped – a new activity completely obscures another activity

The stopped activity is no longer visible

State is maintained

destroyed – the activity must be completely restarted and the state information must be restored

Winter 2020 CS260 - Intro to Java & Android 25

Page 26: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

Activity Skeleton

Figure 04.13

Winter 2020 CS260 - Intro to Java & Android 26

Page 27: CS260 Intro to Java & Android 04.Android Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/04.AndroidIntro.IP.pdfAndroid Application Specifics Android is a multi-user Linux system

ActivityLifeCycleDemo Application

Copy the Android Project ActivityLifeCycleAS353from CS260-01Public

1. Place the file in AndroidStudioProjects on your local machine

2. Let’s take a look at the source code

3. Run the application

Q1: What is the difference between hitting the home button (HOME) and back button (ESC) ?

Q2: What is Log.v and how can it be used?

Winter 2020 CS260 - Intro to Java & Android 27