38
CS5103 Software Engineering Lecture 08 Android Development II

CS5103 Software Engineering Lecture 08 Android Development II

Embed Size (px)

Citation preview

Page 1: CS5103 Software Engineering Lecture 08 Android Development II

CS5103 Software

Engineering

Lecture 08Android Development II

Page 2: CS5103 Software Engineering Lecture 08 Android Development II

2

Last class

Projects Process and Evaluation

Requirements Calendar

SMS Messenger Contact Manager Book Shelf

Android Development I Android system & Emulator

Environment Setup

Page 3: CS5103 Software Engineering Lecture 08 Android Development II

3

Today’s class

Basic Android Development Structure of an android project

Basic UI Components

Activities and Lifecycle

Action bars

Page 4: CS5103 Software Engineering Lecture 08 Android Development II

4

Play with Emulator

Set environment variables On the Emulator Android Debug Bridge Telnet Interface

Page 5: CS5103 Software Engineering Lecture 08 Android Development II

5

Set environment variables

Set Android_SDK_Home: %Android_SDK_Home%/.android will be the

place all virtual devices stored

You may set your sdk directory as Android_SDK_Home

Add dirs to PATH env variable Your sdk directory/tools

Your sdk directory/platform-tools

Page 6: CS5103 Software Engineering Lecture 08 Android Development II

6

Structure of an Android Project

Source code Generated code Manifest file Resource files

Page 7: CS5103 Software Engineering Lecture 08 Android Development II

7

Source code & generated code

Source code The code you will work on

Activities

Typically mainly implementing system call backs

Generated code The code generated by the system according to your

resource files

R.java Including references to all fields in resource files Do not edit it

Page 8: CS5103 Software Engineering Lecture 08 Android Development II

8

Main components of android apps

Main Components Activities

Services

Broadcast Receivers

They usually can be executed directly with intents

Intents Messages sent and received between

components

System intents

Page 9: CS5103 Software Engineering Lecture 08 Android Development II

9

Activities

Most important component of an android app An activity is basically everything showed at one

time on the screen

Like window or frame in the desktop UI applications

Explore an app by trigger different activities

An activity has its own resource files

Page 10: CS5103 Software Engineering Lecture 08 Android Development II

10

Manifest file

Package name Versions targeted at Activities Intent filters Permissions

Uses-permissions

Declare permissions

Page 11: CS5103 Software Engineering Lecture 08 Android Development II

11

Permissions

System permissions Network

Reading/Writing/Sending/Receiving sms

Making/Receiving calls

Reading/writing contacts

Reading/writing SD cards

http://developer.android.com/reference/android/Manifest.permission.html

User defined permissions

Page 12: CS5103 Software Engineering Lecture 08 Android Development II

12

Resource files

Source code The code you will work on

Activities

Typically mainly implementing system call backs

Generated code The code generated by the system according to your

resource files

R.java Including references to all fields in resource files Do not edit it

Page 13: CS5103 Software Engineering Lecture 08 Android Development II

13

Resource files

Drawable Icons used in the app

Layout An xml file describing the layout of an activity Eclipse-ADT provides a graphics layout, and

maintain the consistency

Menu An xml file describing the menu of an activity

Page 14: CS5103 Software Engineering Lecture 08 Android Development II

14

Resource files

Values Dimensions

Strings

Styles (color, fonts, etc.)

A project usually has multiple value folders for different targeted devices Dimensions for different device size

Strings for different languages

Page 15: CS5103 Software Engineering Lecture 08 Android Development II

15

Today’s class

Basic Android Development Play with Emulator

Structure of an android project

Basic UI Components

Activities and Lifecycle

Action bars

Page 16: CS5103 Software Engineering Lecture 08 Android Development II

16

Basic UI Components

Views Basic UI component

Button, Checkbox, MenuItem, etc.

ViewGroup A invisible container that holds views

Determines how views are placed in the container

LinearLayout, GridLayout, AbsoluteLayout, etc.

Page 17: CS5103 Software Engineering Lecture 08 Android Development II

17

Views and View Groups

Page 18: CS5103 Software Engineering Lecture 08 Android Development II

18

Basic UI Components

Views Basic UI component

Button, Checkbox, MenuItem, etc.

ViewGroup A invisible container that holds views

Determines how views are placed in the container

LinearLayout, RelativeLayout, GridView, ListView

Page 19: CS5103 Software Engineering Lecture 08 Android Development II

19

Add views/viewgroups to a project

All views and view groups can be added by Editing layout resource file of an activity

(recommended) Edit the graphics layout

Set main attributes Height Width Text / hint

Page 20: CS5103 Software Engineering Lecture 08 Android Development II

20

Layouts

Linear layouts Views are placed in a vertical or horizontal

list

The default size of views are “wrap_content”

You may set weight for views to make them occupy more spaces

Page 21: CS5103 Software Engineering Lecture 08 Android Development II

21

Layouts

Relative layouts Views are placed relative to their neighbors

Views are places from top-left corner by default

Avoid too much hierarchies

Page 22: CS5103 Software Engineering Lecture 08 Android Development II

22

Layouts

List Views Bind to ListActivity

A vertical list of scrollable items

Use Adaptor to build and manage items

Page 23: CS5103 Software Engineering Lecture 08 Android Development II

23

Layouts

Grid Views Display items in a two-dimensional scrollable

grid

Use adaptors to build view and manage items

Page 24: CS5103 Software Engineering Lecture 08 Android Development II

24

An example of input and display messages

Page 25: CS5103 Software Engineering Lecture 08 Android Development II

25

Respond to UI components

Add the onclick attribute The content of the attribute is the name

of method to be executed Access data in the current activity

Find view by id

Do not forget to set an id for the views you want to get data from

Page 26: CS5103 Software Engineering Lecture 08 Android Development II

26

Respond to UI components

Requirement to the responding method Public

Return type is void

Has a single parameter: android.view.View

Otherwise the system will not be able to call the responding method

Page 27: CS5103 Software Engineering Lecture 08 Android Development II

27

Start another activity

Create a new activity Add it to multiple places (done

automatically by eclipse) Source code

Manifest file

Resource files for the new activity

Generate an intent to start the activity

Page 28: CS5103 Software Engineering Lecture 08 Android Development II

28

Activity life cycles

Create

Start The activity become fully visible to the user

Pause: The activity is partially visible, e.g., pop-up window

Resume The activity is fully visible again

Stop The activity is hidden

Destroy

Page 29: CS5103 Software Engineering Lecture 08 Android Development II

29

Activity life cycles

Page 30: CS5103 Software Engineering Lecture 08 Android Development II

30

Menus

Menus are by default in the project Each activity has a menu resource file A empty action called settings is listed in

the menu Since phone screens are quite small,

menu provide a place to put extra buttons that cannot be put onto the screen

Page 31: CS5103 Software Engineering Lecture 08 Android Development II

31

Add items to menus

Add item to the resource file Add ids to each item

Implement the method: onOptionsItemSelected(MenuItem item)

Page 32: CS5103 Software Engineering Lecture 08 Android Development II

32

Today’s class

Basic Android Development Play with Emulator

Structure of an android project

Basic UI Components

Activities and Lifecycle

Action bars

Page 33: CS5103 Software Engineering Lecture 08 Android Development II

33

Thanks!

Page 34: CS5103 Software Engineering Lecture 08 Android Development II

34

Start an emulator

In eclipse Go to device manager

Click Start

In command line emulator –avd ‘virtual device name’

Page 35: CS5103 Software Engineering Lecture 08 Android Development II

35

On the emulator

Use your keyboard to input text

Useful Shortcuts Home -> Home

Esc -> back

F2 -> Menu

Ctrl + F12 -> change orientation

Manage Apps Go to menu -> manage apps

Operating everything as on a phone

Page 36: CS5103 Software Engineering Lecture 08 Android Development II

36

Adb interface

A very useful tool for controlling emulator adb devices: show all running emulators

Install and uninstall apps

adb push & pull: transfer data between the host system and the emulator

adb logcat: print the system log of the emulator

adb shell: access the file system of the emulator Manage files in the virtual device adb shell pm: List all installed apps

Page 37: CS5103 Software Engineering Lecture 08 Android Development II

37

Telnet interface telnet localhost <port number>

Simulate a number of events Phone calls

gsm call <number>

SMS sms send <number> <“text”>

Power level power status charging / not-charging, power capacity <percentage>

Set GPS geo fix <longitude> <latitude>

Page 38: CS5103 Software Engineering Lecture 08 Android Development II

38

Today’s class

Basic Android Development Play with Emulator

Structure of an android project

Basic UI Components

Activities and Lifecycle

Action bars