Você está na página 1de 82

Android Fundamentals

abelski

Introduction

2008 Haim Michael

What is Android?
Android is a software platform that delivers a complete set of software for mobile devices, including an operating system, a middle-ware and key mobile applications (Google).

2008 Haim Michael

The Android SDK


The Android platform is available to Java programmers through a software development kit, known as the Android SDK. The Android SDK supports most of the Java platform Standard Edition (Java SE) except for the Abstract Windowing Toolkit (AWT) and Swing. Instead of the AWT and Swing, the Android SDK has its own extension modern UI framework.
2008 Haim Michael

The Android JVM


The Android platform has its own optimized Java Virtual Machine, known as Dalvik VM.

2008 Haim Michael

The Android Software Stack


User Applications Java Libraries Dalvik VM Core C Libraries Linux

2008 Haim Michael

Android Development Tools (ADT)


The Android Development Tools (ADT) is an Eclipse plug in. The Android SDK is shipped with the ADT.

2008 Haim Michael

Android User Interface


The Android UI, Microsoft Sliverlight, Mozilla XML User Interface (XUL) and JavaFX belong to the fourth generation UI frameworks, in which the UI is declarative and independently themed. Developing a Java application for the Android platform we declare the user interface in XML files. Screens are usually activities that comprise multiple views the user need to go through.
2008 Haim Michael

Installing the Development Tools


The first steps include installing the Java Development Kit (JDK), the Eclipse IDE and the Android SDK. Once installed, there is a need to install the Android Development Tools plug in (ADT) into our Eclipse IDE installation.

2008 Haim Michael

The Android Content Providers


Through the 'Content Provider' mechanism we can share data without exposing the underlying storage and the underlying implementation. Through 'Content Provider' our application can use other applications' data and other applications can use ours.

2008 Haim Michael

The Android Services


The android service is similar in its nature to services we know from the windows operating system. Local services are accessible by activities that belong to the same application to which the service belong. Remote services are accessible by activities that belong to other applications as well. We can use already existing services as well as creating our own by declaring a class that extends Service.
2008 Haim Michael

The Android Intents


The Android SDK presents a new and unique concept. The Intent defines an intention to do a specific work (e.g. send a message, start a service, launch an activity, dial a number, answer a phone call etc.). The intents might be initiated by your applications as well as by the system (e.g. an intention that represents an arriving message).

2008 Haim Michael

The Android Intents


The android intent defines an intention to do some work. We can use intents to perform various tasks, such as 'broadcasting a message', 'starting a service', 'launching an activity', 'displaying a web page' etc.

2008 Haim Michael

The Android Activities


The android activity is a user interface single screen in our application. Each android activity usually contains one or more views.

2008 Haim Michael

The Android Views


The android view is kind of a canvas self drawn on the screen.

2008 Haim Michael

The Android Configuration File


The AndroidManifest.xml file is the basic configuration file that defines our application. It plays a similar role to web.xml in Java EE web application. This file lists the android application components, such as activities, content providers and services.

2008 Haim Michael

Hello World
Start a new project in your Eclipse IDE. It should be an Android project.
Make sure you choose a meaningful application name. This is the name that will be shown in the application's title bar.

2008 Haim Michael

2008 Haim Michael

Hello World
We can now write our code.
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello World!"); setContentView(tv); }

2008 Haim Michael

2008 Haim Michael

Hello World
In order to run this application we first need to create a configuration. Select 'Run' 'Run Configuration'. Make sure in the configuration window to select the android project you are now working on. Click 'Apply' and 'Run'.

2008 Haim Michael

2008 Haim Michael

2008 Haim Michael

2008 Haim Michael

Android Application Artifacts


The android applications include the following artifacts:
The 'AndroidManifest.xml' File (mandatory) This is the android application descriptor file. Similarly to web.xml when developing Java EE web applications. This file defines the activities, the content providers, the services, and the intent receivers of the application. The 'src' Folder (mandatory) This folder includes all the source code of the applications. The 'assets' Folder (optional) This folder includes an arbitrary collection of folders and files the application uses.

2008 Haim Michael

Android Application Artifacts


The 'res' Folder (mandatory) This folder includes the following sub folders: drawable, anim, layout, values, xml and raw. The 'drawable' optional folder contains the images and the images descriptor files the application uses. The 'anim' optional folder contains the XML descriptor files that describe the animation used by the application. The 'layout' optional folder contains the XML descriptor files that define the application views. The 'values' optional folder contains other resources, such as strings, styles and colors. The 'xml' optional folder contains additional XML files used by the application. The 'raw' optional folder contains additional data mostly none XML one, the application needs. .

2008 Haim Michael

The Android Asset Packaging Tool


This tool is used to pack all files that compose an android application into one binary file.

2008 Haim Michael

The Entry Point Activity


The android manifest file defines an entry point activity, also known as the 'Top Level Activity', marked using an intent-filter element that looks as the following:
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>

When the user tries to run an application, the mobile telephone reads the application manifest file and starts running the activity \ activities that include the MAIN action with the LAUNCHER category.

2008 Haim Michael

2008 Haim Michael

The Entry Point Activity


When the android environment finds the activity it wants to run it resolves that activity into an actual class. The android environment combines the root package name and the activity name.

2008 Haim Michael

2008 Haim Michael

The Entry Point Activity


Once the environment identifies the entry point it starts running the activity by calling the onCreate() method on that activity.

2008 Haim Michael

2008 Haim Michael

The Intent
Activities are usually started with an intent. Each activity is defined using the <activity> element, that may include the <intent-filter> element defining the intent that starts it.

2008 Haim Michael

Calling Other Activities


When a given activity starts it can call another one. Different methods allow calling another activity.
startActivity(Intent intent) startActivityForResult(Intent intent, int requestCode) startActivityFromChild(Activity child, Intent intent, int requestCode) startActivity(Intent) startActivityForResult(Intent, int) startActivityIfNeeded(Intent intent, int requestCode) startNextMatchingActivity(Intent intent)

2008 Haim Michael

The Activities Stack


The activities have a life cycle and all activities are maintained on an activity stack. The running activity is on top. When a running activity starts another one then the running activity moves down the stack and a new activity moves to the top.

2008 Haim Michael

Paused & Stopped Activities


Activities lower in the stack can be called 'paused' or 'stopped' activities. A paused activity is partially (or fully) visible. A stopped activity is not visible. The execution environment is allowed to kill paused and stopped activities if their resources are needed by other activities.

2008 Haim Michael

The SQLite Database


The android platform includes the SQLite database. The SQLiteDatabase class describes this database.

2008 Haim Michael

System Management
The life cycle of an android application is managed by the system, based on the users needs and the available resources.
The system own judgment might cause strange situations (e.g. the user might ask to start running the web browser and the system might prevent that). Current running activities will get the highest priority. Paused or stopped activities (on the other hand) are getting a lower priority and the system might shut them down if it needs their resources.

2008 Haim Michael

Separated Processes
Each application on the android platform runs in a separated process. Each process is running with its own virtual machine. The isolation of each application into a separated process allows the system to allocate different priorities to each one of them.
This way, the execution of high priority applications as getting an incoming phone call is ensured.

2008 Haim Michael

Component & Integration Architecture


The user can move from one application to another. The system is responsible for saving the state of each application allowing the user to return back to it at a later stage. The system saves meta data on every activity before starting another. When memory becomes an issue the system is allowed to independently shut down a running activity and resume it when necessary.
2008 Haim Michael

2008 Haim Michael

06/28/10

Android Fundamentals

abelski
06/28/10 2008 Haim Michael 1

2008 Haim Michael

2008 Haim Michael

06/28/10

Introduction

06/28/10

2008 Haim Michael

2008 Haim Michael

2008 Haim Michael

06/28/10

What is Android?
Android is a software platform that delivers a complete set of software for mobile devices, including an operating system, a middle-ware and key mobile applications (Google).

06/28/10

2008 Haim Michael

2008 Haim Michael

2008 Haim Michael

06/28/10

The Android SDK


The Android platform is available to Java programmers through a software development kit, known as the Android SDK. The Android SDK supports most of the Java platform Standard Edition (Java SE) except for the Abstract Windowing Toolkit (AWT) and Swing. Instead of the AWT and Swing, the Android SDK has its own extension modern UI framework.
06/28/10 2008 Haim Michael 4

2008 Haim Michael

2008 Haim Michael

06/28/10

The Android JVM


The Android platform has its own optimized Java Virtual Machine, known as Dalvik VM.

06/28/10

2008 Haim Michael

2008 Haim Michael

2008 Haim Michael

06/28/10

The Android Software Stack


User Applications Java Libraries Dalvik VM Core C Libraries Linux

06/28/10

2008 Haim Michael

2008 Haim Michael

2008 Haim Michael

06/28/10

Android Development Tools (ADT)


The Android Development Tools (ADT) is an Eclipse plug in. The Android SDK is shipped with the ADT.

06/28/10

2008 Haim Michael

2008 Haim Michael

2008 Haim Michael

06/28/10

Android User Interface


The Android UI, Microsoft Sliverlight, Mozilla XML User Interface (XUL) and JavaFX belong to the fourth generation UI frameworks, in which the UI is declarative and independently themed. Developing a Java application for the Android platform we declare the user interface in XML files. Screens are usually activities that comprise multiple views the user need to go through.
06/28/10 2008 Haim Michael 8

2008 Haim Michael

2008 Haim Michael

06/28/10

Installing the Development Tools


The first steps include installing the Java Development Kit (JDK), the Eclipse IDE and the Android SDK. Once installed, there is a need to install the Android Development Tools plug in (ADT) into our Eclipse IDE installation.

06/28/10

2008 Haim Michael

2008 Haim Michael

2008 Haim Michael

06/28/10

The Android Content Providers


Through the 'Content Provider' mechanism we can share data without exposing the underlying storage and the underlying implementation. Through 'Content Provider' our application can use other applications' data and other applications can use ours.

06/28/10

2008 Haim Michael

10

2008 Haim Michael

10

2008 Haim Michael

06/28/10

The Android Services


The android service is similar in its nature to services we know from the windows operating system. Local services are accessible by activities that belong to the same application to which the service belong. Remote services are accessible by activities that belong to other applications as well. We can use already existing services as well as creating our own by declaring a class that extends Service.
06/28/10 2008 Haim Michael 11

2008 Haim Michael

11

2008 Haim Michael

06/28/10

The Android Intents


The Android SDK presents a new and unique concept. The Intent defines an intention to do a specific work (e.g. send a message, start a service, launch an activity, dial a number, answer a phone call etc.). The intents might be initiated by your applications as well as by the system (e.g. an intention that represents an arriving message).

06/28/10

2008 Haim Michael

12

2008 Haim Michael

12

2008 Haim Michael

06/28/10

The Android Intents


The android intent defines an intention to do some work. We can use intents to perform various tasks, such as 'broadcasting a message', 'starting a service', 'launching an activity', 'displaying a web page' etc.

06/28/10

2008 Haim Michael

13

2008 Haim Michael

13

2008 Haim Michael

06/28/10

The Android Activities


The android activity is a user interface single screen in our application. Each android activity usually contains one or more views.

06/28/10

2008 Haim Michael

14

2008 Haim Michael

14

2008 Haim Michael

06/28/10

The Android Views


The android view is kind of a canvas self drawn on the screen.

06/28/10

2008 Haim Michael

15

2008 Haim Michael

15

2008 Haim Michael

06/28/10

The Android Configuration File


The AndroidManifest.xml file is the basic configuration file that defines our application. It plays a similar role to web.xml in Java EE web application. This file lists the android application components, such as activities, content providers and services.

06/28/10

2008 Haim Michael

16

2008 Haim Michael

16

2008 Haim Michael

06/28/10

Hello World
Start a new project in your Eclipse IDE. It should be an Android project.
Make sure you choose a meaningful application name. This is the name that will be shown in the application's title bar.

06/28/10

2008 Haim Michael

17

2008 Haim Michael

17

2008 Haim Michael

06/28/10

06/28/10

2008 Haim Michael

18

2008 Haim Michael

18

2008 Haim Michael

06/28/10

Hello World
We can now write our code.
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello World!"); setContentView(tv); }

06/28/10

2008 Haim Michael

19

2008 Haim Michael

19

2008 Haim Michael

06/28/10

06/28/10

2008 Haim Michael

20

2008 Haim Michael

20

2008 Haim Michael

06/28/10

Hello World
In order to run this application we first need to create a configuration. Select 'Run' 'Run Configuration'. Make sure in the configuration window to select the android project you are now working on. Click 'Apply' and 'Run'.

06/28/10

2008 Haim Michael

21

2008 Haim Michael

21

2008 Haim Michael

06/28/10

06/28/10

2008 Haim Michael

22

2008 Haim Michael

22

2008 Haim Michael

06/28/10

06/28/10

2008 Haim Michael

23

2008 Haim Michael

23

2008 Haim Michael

06/28/10

06/28/10

2008 Haim Michael

24

2008 Haim Michael

24

2008 Haim Michael

06/28/10

Android Application Artifacts


The android applications include the following artifacts:
The 'AndroidManifest.xml' File (mandatory) This is the android application descriptor file. Similarly to web.xml when developing Java EE web applications. This file defines the activities, the content providers, the services, and the intent receivers of the application. The 'src' Folder (mandatory) This folder includes all the source code of the applications. The 'assets' Folder (optional) This folder includes an arbitrary collection of folders and files the application uses.

06/28/10

2008 Haim Michael

25

The AndroidManifest.xml file is also used to define the permissions required by the application as well as the permissions granted to others.

2008 Haim Michael

25

2008 Haim Michael

06/28/10

Android Application Artifacts


The 'res' Folder (mandatory) This folder includes the following sub folders: drawable, anim, layout, values, xml and raw. The 'drawable' optional folder contains the images and the images descriptor files the application uses. The 'anim' optional folder contains the XML descriptor files that describe the animation used by the application. The 'layout' optional folder contains the XML descriptor files that define the application views. The 'values' optional folder contains other resources, such as strings, styles and colors. The 'xml' optional folder contains additional XML files used by the application. The 'raw' optional folder contains additional data mostly none XML one, the application needs. .

06/28/10

2008 Haim Michael

26

2008 Haim Michael

26

2008 Haim Michael

06/28/10

The Android Asset Packaging Tool


This tool is used to pack all files that compose an android application into one binary file.

06/28/10

2008 Haim Michael

27

The AndroidManifest.xml file is also used to define the permissions required by the application as well as the permissions granted to others.

2008 Haim Michael

27

2008 Haim Michael

06/28/10

The Entry Point Activity


The android manifest file defines an entry point activity, also known as the 'Top Level Activity', marked using an intent-filter element that looks as the following:
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>

When the user tries to run an application, the mobile telephone reads the application manifest file and starts running the activity \ activities that include the MAIN action with the LAUNCHER category.

06/28/10

2008 Haim Michael

28

The AndroidManifest.xml file is also used to define the permissions required by the application as well as the permissions granted to others.

2008 Haim Michael

28

2008 Haim Michael

06/28/10

06/28/10

2008 Haim Michael

29

2008 Haim Michael

29

2008 Haim Michael

06/28/10

The Entry Point Activity


When the android environment finds the activity it wants to run it resolves that activity into an actual class. The android environment combines the root package name and the activity name.

06/28/10

2008 Haim Michael

30

The AndroidManifest.xml file is also used to define the permissions required by the application as well as the permissions granted to others.

2008 Haim Michael

30

2008 Haim Michael

06/28/10

06/28/10

2008 Haim Michael

31

2008 Haim Michael

31

2008 Haim Michael

06/28/10

The Entry Point Activity


Once the environment identifies the entry point it starts running the activity by calling the onCreate() method on that activity.

06/28/10

2008 Haim Michael

32

The AndroidManifest.xml file is also used to define the permissions required by the application as well as the permissions granted to others.

2008 Haim Michael

32

2008 Haim Michael

06/28/10

06/28/10

2008 Haim Michael

33

2008 Haim Michael

33

2008 Haim Michael

06/28/10

The Intent
Activities are usually started with an intent. Each activity is defined using the <activity> element, that may include the <intent-filter> element defining the intent that starts it.

06/28/10

2008 Haim Michael

34

The AndroidManifest.xml file is also used to define the permissions required by the application as well as the permissions granted to others.

2008 Haim Michael

34

2008 Haim Michael

06/28/10

Calling Other Activities


When a given activity starts it can call another one. Different methods allow calling another activity.
startActivity(Intent intent) startActivityForResult(Intent intent, int requestCode) startActivityFromChild(Activity child, Intent intent, int requestCode) startActivity(Intent) startActivityForResult(Intent, int) startActivityIfNeeded(Intent intent, int requestCode) startNextMatchingActivity(Intent intent)

06/28/10

2008 Haim Michael

35

2008 Haim Michael

35

2008 Haim Michael

06/28/10

The Activities Stack


The activities have a life cycle and all activities are maintained on an activity stack. The running activity is on top. When a running activity starts another one then the running activity moves down the stack and a new activity moves to the top.

06/28/10

2008 Haim Michael

36

2008 Haim Michael

36

2008 Haim Michael

06/28/10

Paused & Stopped Activities


Activities lower in the stack can be called 'paused' or 'stopped' activities. A paused activity is partially (or fully) visible. A stopped activity is not visible. The execution environment is allowed to kill paused and stopped activities if their resources are needed by other activities.

06/28/10

2008 Haim Michael

37

2008 Haim Michael

37

2008 Haim Michael

06/28/10

The SQLite Database


The android platform includes the SQLite database. The SQLiteDatabase class describes this database.

06/28/10

2008 Haim Michael

38

2008 Haim Michael

38

2008 Haim Michael

06/28/10

System Management
The life cycle of an android application is managed by the system, based on the users needs and the available resources.
The system own judgment might cause strange situations (e.g. the user might ask to start running the web browser and the system might prevent that). Current running activities will get the highest priority. Paused or stopped activities (on the other hand) are getting a lower priority and the system might shut them down if it needs their resources.

06/28/10

2008 Haim Michael

39

2008 Haim Michael

39

2008 Haim Michael

06/28/10

Separated Processes
Each application on the android platform runs in a separated process. Each process is running with its own virtual machine. The isolation of each application into a separated process allows the system to allocate different priorities to each one of them.
This way, the execution of high priority applications as getting an incoming phone call is ensured.

06/28/10

2008 Haim Michael

40

2008 Haim Michael

40

2008 Haim Michael

06/28/10

Component & Integration Architecture


The user can move from one application to another. The system is responsible for saving the state of each application allowing the user to return back to it at a later stage. The system saves meta data on every activity before starting another. When memory becomes an issue the system is allowed to independently shut down a running activity and resume it when necessary.
06/28/10 2008 Haim Michael 41

2008 Haim Michael

41

Você também pode gostar