Grupo de Usuários de Tecnologia Google de São Paulo
Android Apps 101David Marques | March 11th, 2010
About Me
Lots of mobile fun :)                            ● Developer tools for Java ME                            ● Mobile Device ...
Lots of mobile fun :)
What is Android?
“Android is a software stack for mobile devices that includes  an operating system, middleware and key applications”
Android App Anatomy
An android application is an island of components          Intent Receiver                                  ●   Applicatio...
How do we tell the system what we  have to offer?
Meet the Manifest     <manifest> <application>    <uses-permission> <activity>  <receiver> <service> <provider>
Meet the Main Thread                       Intent Receiver            ●   Managed Life Cycle                              ...
What does he meanwith GET AWAY FROM THE MAIN THREAD       ??????
Android Security Model
●    Proven security model●    Process isolation by   UID●    Permission based model
Basic Building Blocks
Activities   ServicesContent       IntentProviders    Receivers
Activities   ServicesContent       IntentProviders    Receivers
Activities
ISa way to represent a particular          operation           IS NOT necessarily a screen element
What is a Task ?
If Activities aremanaged components  there must be a      life cycle?
How does an Activity look like?     Activity.java + layout.xml
Activity.javapublic class Hello extends Activity {  @Override  public void onCreate(Bundle savedInstanceState) {     super...
layout.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" a...
Activities   ServicesContent       IntentProviders    Receivers
Content Providers
Basic Concepts● Content providers store and retrieve data and make itaccessible to all applications;●   Theyre the only wa...
Hows does it looks like ?public class ExampleProvider extends ContentProvider {      public Cursor query (Uri uri, String[...
Activities   ServicesContent       IntentProviders    Receivers
Services
A Service is an application component thatcan perform long-running operations in the                 background;    It doe...
Started   ServicesWhen the operationis done, the serviceshould stop itself.
Started   ServicesWhen the operationis done, the serviceshould stop itself.
Started   ServicesWhen the operationis done, the servicemust stop itself.
Hows does it looks like ?public class ExampleService extends Service {    public void onCreate() {      // The service is ...
Well well well,long-running operations           +      background      And he said GET      AWAY FROM THE       MAIN THRE...
“Caution: A service runs in the main thread of its hostingprocess the service does not create its own thread and doesnot r...
“Caution: A service runs in the main thread of its hostingprocess the service does not create its own thread and doesnot r...
“Caution: A service runs in the main thread of its hostingprocess the service does not create its own thread and doesnot r...
I__E__ _E__I_E
INTENT SERVICE                            ______ _______                       ●   Creates a default worker thread;       ...
Bound    Service●Allows applicationcomponents to bindto it●Expose applicationsfunctionality
Hows does it looks like ?public class ExampleService extends Service {  IBinder mBinder;    // interface for clients that ...
Activities   ServicesContent       IntentProviders    Receivers
Intent Receivers
Its all about Intents!“Intents is the mechanism by which the applications talk toeach other and with the system”     ACTIO...
Hows does it looks like ?public class ExampleReceiver extends BroadcastReceiver {      public void onReceive(Context conte...
DOs
●   DO run on a background thread if its not UI related;●   DO consider multiple form factors;●   DO respect the user pref...
DONTs
●   DONT replace the back, home, search and menu features;●   DONT remove the status bar unless you need to;●   DONT alloc...
Development environment
Eclipse + ADT =  http://eclipse.org   http://developer.android.com/sdk/eclipse-adt.html edit+debug+log+compile+package+nav...
Questions ?
Lets Code :)
Android Apps 101David Marques | March 11th, 2010
Android101
Android101
Android101
Upcoming SlideShare
Loading in …5
×

Android101

2,345 views

Published on

Android 101 talk at the GTUG meeting in São Paulo/Brazil.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,345
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
56
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Android101

  1. 1. Grupo de Usuários de Tecnologia Google de São Paulo
  2. 2. Android Apps 101David Marques | March 11th, 2010
  3. 3. About Me
  4. 4. Lots of mobile fun :) ● Developer tools for Java ME ● Mobile Device Emulator ● Eclipse Foundation Committer
  5. 5. Lots of mobile fun :)
  6. 6. What is Android?
  7. 7. “Android is a software stack for mobile devices that includes an operating system, middleware and key applications”
  8. 8. Android App Anatomy
  9. 9. An android application is an island of components Intent Receiver ● Application Package ● Android Manifest Content Provider ●Defaults to 1 process per application Activity Service ●Defaults to 1 thread Linux Process per application Application
  10. 10. How do we tell the system what we have to offer?
  11. 11. Meet the Manifest <manifest> <application> <uses-permission> <activity> <receiver> <service> <provider>
  12. 12. Meet the Main Thread Intent Receiver ● Managed Life Cycle Content ● System events, life Provider cycle managementAndroid System Looper Service callbacks and UI updates happen on the Main Thread Message Queue Activity ● Get away from it :) Main Thread Linux Process
  13. 13. What does he meanwith GET AWAY FROM THE MAIN THREAD ??????
  14. 14. Android Security Model
  15. 15. ● Proven security model● Process isolation by UID● Permission based model
  16. 16. Basic Building Blocks
  17. 17. Activities ServicesContent IntentProviders Receivers
  18. 18. Activities ServicesContent IntentProviders Receivers
  19. 19. Activities
  20. 20. ISa way to represent a particular operation IS NOT necessarily a screen element
  21. 21. What is a Task ?
  22. 22. If Activities aremanaged components there must be a life cycle?
  23. 23. How does an Activity look like? Activity.java + layout.xml
  24. 24. Activity.javapublic class Hello extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }}● AndroidManifest.xml<activity android:name=".Hello" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>
  25. 25. layout.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /></LinearLayout>
  26. 26. Activities ServicesContent IntentProviders Receivers
  27. 27. Content Providers
  28. 28. Basic Concepts● Content providers store and retrieve data and make itaccessible to all applications;● Theyre the only way to share data across applications;● Theres no common storage area that all Android packagescan access.● How a content provider actually stores its data under thecovers is up to its designer! audio - video - images - personal contact information
  29. 29. Hows does it looks like ?public class ExampleProvider extends ContentProvider { public Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Returns data to the caller via a Cursor object } public Uri insert (Uri uri, ContentValues values) { // Inserts new data into the content provider } public int update (Uri uri, ContentValues values, String selection, String[] selectionArgs) { // Updates existing data in the content provider } public int delete (Uri uri, String selection, String[] selectionArgs) { // Deletes data from the content provider }}<provider android:name="com.example.autos.AutoInfoProvider" android:authorities="com.example.autos.autoinfoprovider" . . . /></provider>
  30. 30. Activities ServicesContent IntentProviders Receivers
  31. 31. Services
  32. 32. A Service is an application component thatcan perform long-running operations in the background; It does not provide an user interface element;
  33. 33. Started ServicesWhen the operationis done, the serviceshould stop itself.
  34. 34. Started ServicesWhen the operationis done, the serviceshould stop itself.
  35. 35. Started ServicesWhen the operationis done, the servicemust stop itself.
  36. 36. Hows does it looks like ?public class ExampleService extends Service { public void onCreate() { // The service is being created } public int onStartCommand(Intent intent, int flags, int startId) { // The service is starting, due to a call to startService() return mStartMode; } public void onDestroy() { // The service is no longer used and is being destroyed }} <service android:name=".ExampleService" />http://developer.android.com/guide/topics/manifest/service-element.html
  37. 37. Well well well,long-running operations + background And he said GET AWAY FROM THE MAIN THREAD ??????
  38. 38. “Caution: A service runs in the main thread of its hostingprocess the service does not create its own thread and doesnot run in a separate process (unless you specify otherwise).”“If your service is going to do any CPU intensive work orblocking operations (such as MP3 playback or networking),you should create a new thread within the service to do thatwork.”http://developer.android.com/guide/topics/fundamentals/services.html
  39. 39. “Caution: A service runs in the main thread of its hostingprocess the service does not create its own thread and doesnot run in a separate process (unless you specify otherwise).”“If your service is going to do any CPU intensive work orblocking operations (such as MP3 playback or networking),you should create a new thread within the service to do thatwork.”http://developer.android.com/guide/topics/fundamentals/services.html
  40. 40. “Caution: A service runs in the main thread of its hostingprocess the service does not create its own thread and doesnot run in a separate process (unless you specify otherwise).”“If your service is going to do any CPU intensive work orblocking operations (such as MP3 playback or networking),you must create a new thread within the service to do thatwork.”http://developer.android.com/guide/topics/fundamentals/services.html
  41. 41. I__E__ _E__I_E
  42. 42. INTENT SERVICE ______ _______ ● Creates a default worker thread; ● Creates a work queue, so you never have to worry about multi-threading; ● Stops the service for you;http://developer.android.com/reference/android/app/IntentService.html
  43. 43. Bound Service●Allows applicationcomponents to bindto it●Expose applicationsfunctionality
  44. 44. Hows does it looks like ?public class ExampleService extends Service { IBinder mBinder; // interface for clients that bind public IBinder onBind(Intent intent) { // A client is binding to the service with bindService() return mBinder; } public boolean onUnbind(Intent intent) { // All clients have unbound with unbindService() return mAllowRebind; } public void onRebind(Intent intent) { // A client is binding to the service with bindService(), // after onUnbind() has already been called }} Android Interface Definition Language (AIDL)http://developer.android.com/guide/topics/fundamentals/bound-services.html
  45. 45. Activities ServicesContent IntentProviders Receivers
  46. 46. Intent Receivers
  47. 47. Its all about Intents!“Intents is the mechanism by which the applications talk toeach other and with the system” ACTION_BOOT_COMPLETED - ACTION_BATTERY_CHANGED - ACTION_POWER_CONNECTED – ACTION_SHUTDOWN – ACTION_MEDIA_MOUNTED – ACTION_NEW_OUTGOING_CALL● Super Fast Life-Cycle: ONLY ONE CHANCE :)● Permissions can be enforced either by the sender orreceiver
  48. 48. Hows does it looks like ?public class ExampleReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { // Thats your ONLY CHANCE to do anything! // Once it returns GAME OVER }}● AndroidManifest.xml<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>...<receiver android:name=".SMSReceiver" android:enabled="true"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter></receiver>http://developer.android.com/reference/android/content/BroadcastReceiver.html
  49. 49. DOs
  50. 50. ● DO run on a background thread if its not UI related;● DO consider multiple form factors;● DO respect the user preferences;● DO consider that you are not running alone on the system;● DO hire a designer;● DO collaborate with other applications;● DO support landscape and portrait;● DO consider publishing on the Android Market
  51. 51. DONTs
  52. 52. ● DONT replace the back, home, search and menu features;● DONT remove the status bar unless you need to;● DONT allocate objects unless you need to;● DONT hold Context object references;● DONT update Widgets toooooooo often;● DONT wake up the system to do nothing;● DONT log sensitive user information;● DONT use non public APIs =X
  53. 53. Development environment
  54. 54. Eclipse + ADT = http://eclipse.org http://developer.android.com/sdk/eclipse-adt.html edit+debug+log+compile+package+navigate file system+analyze memory+ edit user interface+emulate device+ localize+analyze performance+...
  55. 55. Questions ?
  56. 56. Lets Code :)
  57. 57. Android Apps 101David Marques | March 11th, 2010

×