Anda di halaman 1dari 26

Android

Introduction Session 1

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 es conocido como el completo sistema operativo Android.

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.

(irectory Structure of an Android ,ro5ect


Project's Name Here go our code stuff :) API that is using the project i!raries needed Resource Folder

Project "anifest Project Icon

(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.

(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 this folder. - 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.

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, you need 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.

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 the application. ;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.

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? + Ao3 do you declare one? + Ao3 do you import it? + s it possible to have the same pac1age name in app@ developed by 6 M and myApplication developed by Bscar?

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.

Emanifest 0mlns<androidFGhttp<88schemas.android.com8ap18res8androidG pac1ageFGcom.e0ample.testandroidpro5ectG android<version*odeFG@G android<version9ameFG@.%G H Euses+sd1 android<minSd1VersionFG@IG android<targetSd1VersionFG@&G 8H Eapplication android<iconFGJdra3able8icKlauncherG android<labelFGJstring8appKnameG android<themeFGJstyle8AppThemeG H Eactivity android<nameFG.MainActivityG android<labelFGJstring8titleKactivityKmainG H Eintent+filterH Eaction android<nameFGandroid.intent.action.MA 9G 8H

About Manifest.0ml

Ecategory android<nameFGandroid.intent.category.'A79*AE6G 8H E8intent+filterH E8activityH E8applicationH E8manifestH

About Manifest.0ml
T ,S< >ou must version your application. Version code is an integer value that represents the version of your application code relative to other versions of your application. This value is used to help other applications determine their compatibility 3ith your application. Also Google ,lay uses it as a basis for identifying the application internally and for handling updates. f you don:t set a ne3 release 3ith a higher integer number, Google ,lay 3on:t ta1e it to advice users that a ne3 release is ready.

About Manifest.0ml
Commonly requested Application Permissions
Permission Internet $rite %&ternal Storage )amera Access Fine ocation Read Phone State Description #he application needs access to the Internet #he application needs to 'rite data to the Secure (igital )ard *S( )ard) #he application needs access to the camera #he application needs access to the +lo!al Positioning S,stem *+PS) location #he application needs to access the state of the phone *such as ringing)

The Anatomy of an Android Application< The A,L ;ile


The cornerstone of Android application development is the application pac1age file format, or the A,L 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 Ao3 do guys you thin1 that are organi=ed all files in that apk?

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.

Android Activities< (efining the 7


An Android activity contains a UI construct t at accomplis es a given userinput tas! via t e smartp one " tablet screen. Android app can have more than one activity. n fact, more comple0 applications usually have one activity for each 7 screen implementation. ;or e0ample if you are programming a game, you might have the follo3ing activities< @. The introductory splash screen 3ith the *ontinue to ,lay Game B6 ,ress -utton to ,lay Game. $. The instructions screen, 3ith the scrolling te0t 7 . C. The high+score screen, 3ith 7 elements that allo3 the user to manage high+ scores entries. D. A player groups screen, 3here users choose 3ho 3ill play the game 3ith them. &. The actual core gameplay screen itself.

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 splash screen /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%onCreate saved"nstance#tate$& setContent'iew (%layout%)ain$& * *

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 a product name or SL7.

Android Services< ,rocessing in the -ac1ground


7nli1e activities, services do not have any visual 7 /thatMs 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 the application, 3hile the userMs 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

-roadcast 6eceivers< Announcements and 9otifications


Broadcast Receivers are communication components that receive messages that are sent bet3een the Android operating system and other application components, or bet3een Android application components themselves. The Android operating system often sends messages regarding the status of 3hatMs going on in real time 3ith the Android phone itself. These are statuses that any Android application may 3ant or even need to 1no3 about in order to protect the application integrity, such as if the phone is about to lose po3er and your app needs to save files. #xamples of Android operatin system+initiated broadcast messages< - A lo3 battery life 3arning - A time =one change notice - A language preference change notice - A message that the camera has been used to snap a picture

-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 thus do not have associated 7 elements. Ao3ever, this does not mean that the broadcast receivers cannot trigger or invo1e a 7 activity in response to the messages that they carry. It is a common practice to ave broadcast receivers trigger UI elements t at alert t e user as to w at is going on wit in t e application. -roadcast receivers are created by e0tending the Android BroadcastReceiver class.

*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 is created 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 phone utilities 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 a number of built+in content provider databases, including< *ontacts mages Audio Video

*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 a ContentResolver 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 content resolver. The other three components 4 activities, services, and broadcast receivers 4 are activated via asynchronous messages called intents.

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, the data t at t e 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, and broadcast 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 3ell defined, as they 3ill be different for each type of Android component.

Android ntent Bb5ects< Messaging for *omponents


$. An activity is started up, or if itMs 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.

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 establish an open connection bet3een the calling component and the service that is being used. This al3ays open, real+time connection bet3een code modules is commonly called binding in programming. '. An Android broadcast receiver component is started by passing an ntent ob5ect to 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

Anda mungkin juga menyukai