project a day 2 android application fundamentals

25
Android Application Fundamentals

Upload: goran-djonovic

Post on 24-May-2015

155 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: Project a day 2   android application fundamentals

Android Application Fundamentals

Page 2: Project a day 2   android application fundamentals

Android concepts

• Activity – visual user interface focused on a single thing a user can do

• Service – no visual interface – they run in the background

• Broadcast receiver – receive and react to broadcast announcements

• Content provider – allow data exchange between applications

• Intent• Manifest

2

Page 3: Project a day 2   android application fundamentals

Applications

• Written in Java• Lunching an application makes it the root of a task (not equal task)• Each application runs in its own process• Each process has its own separate VM• Each application is assigned a unique Linux user ID – by default files

of that application are only visible to that application (can be explicitly exported)• Android manages memory by process prioritization and it has a

license to kill

3

Page 4: Project a day 2   android application fundamentals

4

Activities

• Basic component of most applications• Most applications have several activities that start each other as

needed• Task is a set of Activities arranged in a stack (example Gmail app)• Stack is independent• Each is implemented as a subclass of the base Activity class• Activities can belong to a process that is killed (what to do then?)

Page 5: Project a day 2   android application fundamentals

Understanding the lifecycle

5

Page 6: Project a day 2   android application fundamentals

Activity states

• Running state• Activity is in the foreground• Full access to resource• Will not be destroyed

• Paused state• Activity is visible but not in the foreground• Retains memory resources• Limited opportunity to perform processing• Not likely to be destroyed

• Stopped state• Activity is not visible• Should be prepared to lose memory resources• Limited opportunity to perform processing• Very likely to be destroyed

6

Page 7: Project a day 2   android application fundamentals

7

Activities – The View

• Each activity has a default window to draw in (although it may prompt for dialogs or notifications)• The content of the window is a view or a group of views (derived

from View or ViewGroup)• Example of views: buttons, text fields, scroll bars, menu items, check

boxes, etc.• View(Group) made visible via Activity.setContentView() method.

Page 8: Project a day 2   android application fundamentals

Activity DEMO

• Activity Lifecycle• Getting Views from the Layout• Menu handling

8

Page 12: Project a day 2   android application fundamentals

Activity

• Activities use methods and resources do describe menus and layout• Menu items normally have at least an id and a title• Use string resource to avoid literal strings• Prefer the onOptionItemSelected method over the onClick Method• All activities must be defined in manifest• Activities are displayed with startActivity method (with intent)• Each Activity manages its own menu

• Activity life time is not the same as process life time

12

Page 13: Project a day 2   android application fundamentals

Views and Layouts

• The role of Views and Layouts• Using Layouts• Linear Layout• Relative Layout• Handling View event Callback

13

Page 14: Project a day 2   android application fundamentals

The role of Views and Layouts

• Android UI is composed of Layouts and Views• Views are UI components• ViewGroups are Views that hold othe

Views

• Layouts are specialized ViewGroups• Describe View positioning• A Layout is commonly the root of the UI• Layouts are often layered within one

another

• You can construct your UI using resource or the code

14

Page 15: Project a day 2   android application fundamentals

Views are positioned based on its parent layout

15

Page 16: Project a day 2   android application fundamentals

Using Layouts

• Linear LayoutLayout_gravity = left/right Layout_weight = numberGravity affects content and layout gravity affects controll

• Relative LayoutRelative to other control (based on id) and/or parent

• Grid Layout• RolumCount• RowCount• Layout_row • Layout_column• Layout_rowSpan• Layout_columnSpan

16

Page 18: Project a day 2   android application fundamentals

18

Services

• Does not have a visual interface• Runs in the background indefinitely • Examples

• Network Downloads• Playing Music• TCP/UDP Server

• You can bind to a an existing service and control its operation

Page 19: Project a day 2   android application fundamentals

19

Broadcast Receivers

• Receive and react to broadcast announcements• Extend the class BroadcastReceiver• Examples of broadcasts:

• Low battery, power connected, shutdown, timezone changed, etc.• Other applications can initiate broadcasts

Page 20: Project a day 2   android application fundamentals

20

Content Providers

• Makes some of the application data available to other applications• It’s the only way to transfer data between applications in Android (no

shared files, shared memory, pipes, etc.)• Extends the class ContentProvider;• Other applications use a ContentResolver object to access the data

provided via a ContentProvider

Page 21: Project a day 2   android application fundamentals

21

Intents

• An intent is an Intent object with a message content.• Activities, services and broadcast receivers are started by

intents. ContentProviders are started by ContentResolvers

Page 22: Project a day 2   android application fundamentals

22

Shutting down components

• Activities• Can terminate itself via finish();• Can terminate other activities it started via finishActivity();

• Services• Can terminate via stopSelf(); or Context.stopService();

• Content Providers• Are only active when responding to ContentResolvers

• Broadcast Receivers• Are only active when responding to broadcasts

Page 23: Project a day 2   android application fundamentals

23

Android Manifest

• Its main purpose in life is to declare the components to the system: <?xml version="1.0" encoding="utf-8"?>

<manifest . . . > <application . . . > <activity android:name="com.example.project.FreneticActivity" android:icon="@drawable/small_pic.png" android:label="@string/freneticLabel" . . . > </activity> . . . </application></manifest>

Page 24: Project a day 2   android application fundamentals

Android Manifest

• Version ID and Version name• Application settings• Permissions• Instrumentation• Use the XML view

24