8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 1/26
Android
Introduction
Session 1
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 2/26
The roots of Android Mucha gente no conoce sobre esto, pero Google no inicio el proyecto
Android. El inicio del sistema operativo Android fue creado por una
empresa en Silicon Valley conocida como “Android, nc.!, "ue fue
comprada por Google en #ulio de $%%&.
'os fundadores de “Android, nc!, provienen de distintas empresas de
nternet, tales como (anger, )ildfire *ommunications, T+Mobile y
)ebTV.
Google los incorporo a su e"uipo para ayudarles a crear lo "ue hoy esconocido como el completo sistema operativo Android.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 3/26
Android rogramming -asics
+ M' /E0tensible Mar1up 'anguage2
Mar1up language that defines a set of rules for encoding documents in
a format that is both human+readable and machine+readable.
+ #ava
Android applications are 3ritten in #ava 4 not the full+blo3n #ava that#$EE /#ava $ Enterprise Edition2 developers are used to, but a subset of
#ava that is sometimes 1no3n as the Dalvik virtual machine.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 4/26
(irectory Structure of an Android ro5ectProject's NameHere go our code stuff :)
API that is using the project
Resource Folder
i!raries needed
Project "anifestProject Icon
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 5/26
(irectory Structure of an Android ro5ect
Every Android pro5ect needs a 6esource folder.
Since Android is very particular about 3here the assets of your pro5ect
are stored 3ithin the pro5ect directory, you need to learn 3here each
belongs early in the game.
Common default resource folders:
- layout. 7 screen layouts go in the 8res8layout folder 3hich holds M'files containing 7 layout definitions 3ritten in M'.
- anim. M' files that define animation go in the 8res8anim folder
- drawable. mages in 9G format /3hich Google prefers2 or the #EG
format /acceptable but not favored by Google2 go into the 8res8dra3able
/screen+dra3able imagery2 folder.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 6/26
(irectory Structure of an Android ro5ect
- values. M' files that define constant values are in the /res/values
- color. M' files that specify related color values for your application :s
7 go in the /res/color folder. ;or e0ample, if your app has complicated
color bundles for different states of a button /a different color for 3hen it
is pressed, focused, or unused2, they 3ill be logically arranged in thisfolder.
- xml. M' files that define custom data constructs are in the /res/menu
folder- raw. Video files that are precompressed go in the /res/raw folder, so
Android 1no3s not to process them further.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 7/26
The values folder
Thin1 of the values folder as holding all of your constant values for your
application in one place. This 3ay, if you need to ad5ust them during
application development and testing, you ma1e the changes in a single
location.
- colors.xml< An M' file that 3ill define the color values to be used in
the app. These allo3 to standardi=e the 7. ;or e0ample, you 3ould
define your bac1ground color. Then, if you decide to t3ea1 it later, youneed to do the t3ea1 in only one place.
- dimmens.xml< An M' file that defines dimension values, such as
standar heights and font si=es for your 7. >ou can then use these values
across your app to ensure it is consistent.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 8/26
The values folder
- arrays.xml< An M' file that defines a series of values to be used
together /1no3n as an array2. ;or e0ample it could be a list of icon files or
a list of options to display to the user.
- strings.xml< An M' file that defines test strings to be used in theapplication. ;or e0ample, you can place any screen titles or the app:s
name here and reference them in your code. f you need to change these
items, you simply do it here rather than in your code.
- styles.xml: An M' file that defines styles to be used in the
application. These styles are then applied to the 7 elements that re"uire
them, so you separate the loo1 of your app from the layout and
functionality. This ma1es your app easier to maintain.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 9/26
About Manifest.0ml
The Android manifest file is stored at the root of your pro5ect and is
named AndroidManifest.xml . Every application must have an Android
Manifest file in its root directory.
)hat does an Android Manifest file provide us?
@. 6eferences to the #ava code you 3ill 3rite for your application,
meaning your #ava pac1age for the application.
+ )hat is a pac1age?
+ o3 do you declare one?
+ o3 do you import it?
+ s it possible to have the same pac1age name in app@ developed by
6M and myApplication developed by Bscar?
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 10/26
About Manifest.0ml
$. The components of the application, such as the activities and
bac1ground services.
C. The declaration of the permissions your application re"uires to run.
D. The minimum level of the Android A that the application re"uires.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 11/26
About Manifest.0mlmanifest 0mlns<androidFhttp<88schemas.android.com8ap18res8android
pac1ageFcom.e0ample.testandroidpro5ect android<version*odeF@ android<version9ameF@.% H
uses+sd1 android<minSd1VersionF@I android<targetSd1VersionF@& 8H
application android<iconFJdra3able8icKlauncher android<labelFJstring8appKname android<themeFJstyle8AppTheme H activity android<nameF.MainActivity android<labelFJstring8titleKactivityKmain H intent+filterH action android<nameFandroid.intent.action.MA9 8H
category android<nameFandroid.intent.category.'A79*E6 8H 8intent+filterH 8activityH 8applicationH8manifestH
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 12/26
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 13/26
About Manifest.0ml
Commonly requested Application Permissions
Permission Description
Internet #he application needs access to the Internet
$rite %&ternal Storage #he application needs to rite data to the Secure (igitalard *S( ard)
amera #he application needs access to the camera
Access Fine ocation #he application needs access to the +lo!al PositioningS,stem *+PS) location
Read Phone State #he application needs to access the state of the phone*such as ringing)
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 14/26
The Anatomy of an Android Application<
The AL ;ile The cornerstone of Android application development is the application
pac1age file format, or the AL file format. )hen you compile and output
an application for distribution to your Android users, the Eclipse (E and
Android S(L output your application file name 3ith an .apk e0tension.
There is only one .apk file.
ncludes< + All of your application code
+ Any ne3 media resources
+ AndroidManifest.0ml file
o3 do guys you thin1 that are organi=ed all files in thatapk ?
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 15/26
Android Application *omponents
There are four main types of components that can be /but do not need
to be2 used 3ithin an Android application<
+ Activities handle the 7 to the smartphone 8 tablet screen
+ Services handle bac1ground processing.
+ -roadcast receivers handle communication in your apps.
+ *ontent providers handle data and database management issues.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 16/26
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 17/26
Android Activities< (efining the 7
f an application has more than one activity, one is mar1ed as the activity that is
presented 3hen the application is launched. n our game e0ample, that is the splashscreen /although it could be the instructions screen2. This activity has an onCreate()
method that calls the main.xml file.
E0ample<
public class MyHelloWorld extends Activity {
/**Called when the activity is first created */
@Override
public void onCreate!undle saved"nstance#tate${
super%onCreatesaved"nstance#tate$& setContent'iew(%layout%)ain$&
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 18/26
Android Activities< (efining the 7
T<
An activity can be full screen, or it can be part of a screen, allo3ing for floating
3indo3s on top of other 3indo3s. An activity can also ma1e use of other 3indo3s.;or instance, an activity might sho3 a pop+up dialog re"uesting that the user enter
information, or it could be display a product information 3indo3 3hen a user clic1s aproduct name or SL7.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 19/26
Android Services<
rocessing in the -ac1ground 7nli1e activities, services do not have any visual 7 /thats 3hat an activity is for2.
Services handle the processing or heavy lifting for your application. They are often
used for doing things that need to be done in the bac!ground or bac! end of theapplication, 3hile the users 3or1s 3ith your 7 in the foreground or front end of your
application.
)hat do you understand as bac1ground? )hat is a MV*?
#xamples<
+ *alculate numeric values + rocess game logic + lay media elements such as video and audio streams
+ ull data from remote net3or1 locations + Transfer data bet3een devices via -luetooth
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 20/26
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 21/26
-roadcast 6eceivers<
Announcements and 9otifications#xamples of application+to+application broadcast messages- An alert that data has finished do3nloading- A message that streaming video media has arrived, is cached, and is ready for the
start of playbac1.
'i1e Android services, broadcast receivers operate in the bac1ground, and thusdo not have associated 7 elements. o3ever, this does not mean that the broadcast
receivers cannot trigger or invo1e a 7 activity in response to the messages that theycarry.
It is a common practice to ave broadcast receivers trigger UI elements tat
alert te user as to wat is going on witin te application.
-roadcast receivers are created by e0tending the Android BroadcastReceiver
class.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 22/26
*ontent roviders<
(ata Management Content providers in Android provide a 3ay to ma1e data available to your
application and to other applications, if that is desired. This can be data that iscreated in and for your o3n application, or it can be data that can be accesed by your
application, but that is created by other applications, or even by the Android phoneutilities themselves.
;or e0ample, an Android phone utility uses a content provider to access the
phone number database that is 1ept 3ithin your smartphone. Android comes 3ith anumber of built+in content provider databases, including<
- *ontacts-
mages- Audio- Video
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 23/26
*ontent roviders<
(ata Management To create your o3n content provider, you e0tend ContentProvider base class,
3hich implements a standard set of methods that are used to store and retrieve data.
Applications access the methods defined by your ContentProvider class 3ith aContentResolver ob5ect, 3hich is used to tal1 to any content provider, in order to
navigate the data that is needed by the application.
A content provider is activated 3hen it receives a re"uest for data from a contentresolver.
The other three components 4 activities, services, and broadcast receivers 4 are
activated via asynchronous messages called intents .
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 24/26
Android ntent Bb5ects<
Messaging for *omponents An ntent ob5ect in Android holds the contents of a message that is sent bet3een
modules, typically to launch them or to send them ne3 tas1 instructions.
;or activities and services, an ntent ob5ect provides an action to be ta!en, thedata tat te action needs to operate on, and optionally, some details or
additional information that may be re"uired for more complicated operations.
>ou communicate 3ith each type of Android component /activity, service, andbroadcast receiver2 using a different set of methods to receive the ntent ob5ect that
is passed to it. ;or this reason, ntent ob5ects are easy to 1eep separate and 3elldefined, as they 3ill be different for each type of Android component.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 25/26
Android ntent Bb5ects<
Messaging for *omponents $. An activity is started up, or if its already started, given a ne3 tas1, by passing
an ntent ob5ect to the Context.startActivity() method.
The Activity class can loo1 at the contents of the ntent ob5ect via getIntent() method, and at subse"uent intent ob5ects via the onNewIntent() method.
%. An Android service component is started by passing an Intent ob5ect to the
Context.startService() method, 3hich then calls the service class onStart() method,and passes it the ntent ob5ect the actions for the service to perform and the data on
3hich perform them.
8/12/2019 Android Sesion 1
http://slidepdf.com/reader/full/android-sesion-1 26/26
Android ntent Bb5ects<
Messaging for *omponents &. f the service is already running and the Intent ob5ect contains ne3 instructions,
then the intent is passed to the Context.bindService() method in order to establishan open connection bet3een the calling component and the service that is being
used.
This al3ays open, real+time connection bet3een code modules is commonlycalled binding in programming.
'. An Android broadcast receiver component is started by passing an ntent ob5ectto the Context.sendBroadcast () method, or optionally to the
Context.sendOrderedBroadcast() method or Context.sendStickyBroadcast ()method. The Intent ob5ect in this case contains the mesage to be ta1en and the data/the message2 on 3hich that action need to be ta1en