Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
From A to Z
Android
Hazem Hagrass
Senior Software Engineer
hazem.hagrass@badrit.com 2
Agenda
● Overview
● Architecture
● Environment Setup
● Project Structure and modules
● Application Components
⚪ Intents an...
Overview
4
Android: The Forgotten History
The Forgotten History
● Created by Andy Rubin, a free, open source mobile platform that any coder
could write for and any ...
The Forgotten History
● He would make his money by selling support for the system security services,
or email management.
6
The Forgotten History
● Rubin discussed the idea with Larry Page(Google's CEO), he didn’t want
money from Page. He already...
The Forgotten History
● When Apple introduced the iPhone in 2007, Google was already working on
Android, its own smartphon...
Who is OHA?
9
Who is OHA?
10
● The OHA was established on 6 November 2007
● it is led by Google with 47 members including mobile handset...
Steve Jobs!
"Everything is a f**king rip off of what we're doing," Jobs said of Android
11
Android powers hundreds of millions of mobile
devices in more than 190 countries around the world.
It's the largest instal...
Market Share
13
● Every day more than 1 million new Android devices are
activated worldwide.
● 1.5 billion downloads a month and growing. ...
Android is open!
Android is Open, it is built on the open Linux Kernel.
it can be liberally extended to incorporate new cu...
All applications are created equal
Android does not differentiate between the phone’s core applications and
third-party ap...
Architecture
17
System Architecture
Architecture
18
Linux Kernel
● The Android OS is derived from Linux Kernel 2.6 and is actually created from
Linux source
● This provides b...
Libraries
● This layer holds the Android native libraries written in C/C++ and offer
capabilities similar to the above lay...
It provides a key component called Dalvik Virtual Machine which is a kind of Java
Virtual Machine specially designed and o...
Application Framework
It is the layer which application developers can leverage in developing Android
applications. The fr...
Your application to be installed on this layer only. Examples of such applications
are Contacts Books, Browser, Games etc....
Environment Setup
24
Installation
Now everything is easy and can be done on one click just by download Android
SDK tools which contains:
● Android Studio ID...
Project Structure
26
Structure and modules
Top Level Folders
27
● Main Project: This would be entire project context (Eclipse Land: Like your
workspace but limited to what's relevant to ...
● Project Module: This is the actual project. ex: HelloWorld if your application
name you gave was HelloWorld.
⚪ build: Th...
● Project Module: This is the actual project. ex: HelloWorld if your application
name you gave was HelloWorld.
⚪ src/res/l...
● AndroidManifest.xml is an interface between the Android OS and your
application.
What is AndroidManifest.xml?
31
What is AndroidManifest.xml?
32
Application Components
33
Intents, activities, services and broadcast receivers
● App components are the essential building blocks of an Android app. Each
component is a different point through which th...
Intents and Intent Filters
Application Components
35
● An Intent is a messaging object you can use to request an action from
another app component or communicate between other...
● Although intents facilitate communication between components in several
ways, there are three fundamental use-cases:
⚪ T...
Intents Types
Explicit intents:
● Used to start a component in your own app, because you know the class
name of the activi...
Intents Types
39
Implicit intents:
● No specific component needed , but instead declare a general action to
perform, which...
Intent contains at least one of:
40
● Component name(optional): The name of the component to start. it's
the critical piec...
Intents Attributes
41
● Category(optional): A string containing additional information about the
kind of component that sh...
Intents Attributes
42
● Data(optional): The URI (a Uri object) that references the data to be
acted on and/or the MIME typ...
Intents Attributes
43
● Flags(optional): The flags may instruct the Android system how to
launch an activity (for example,...
● Used to register specific component as being capable for doing specific action
on a set of data.
● To advertise which im...
● Each intent filter is defined by an <intent-filter> element in the app's
manifest file, nested in the corresponding app ...
46
Ready to code!
Activities
Application Components
47
An Activity is an application component that provides a single screen with which
users can interact in order to do somethi...
Lifecycle
49
Example
50
How to use?
51
You can start an activity by calling startActivity(), passing it an Intent that
describes the activity you ...
52
Ready to code!
Services
Application Components
53
● A service is a component that runs in the background to perform long-running
operations without needing to interact with...
● A service can essentially take two forms:
a. Started(Unbounded): A service is "started" when an application
component (s...
● A service can essentially take two forms:
b. Bound: A service is "bound" when an application component binds to it by
ca...
● A service runs in the main thread of its hosting process.
● The service does not create its own thread and does not run ...
Lifecycle(cont’)
58
● If the service is bound to an activity that has user focus, then it's less
likely to be killed, and if the service is de...
Example
60
Services: Started Service
Application Components
61
⚪ Service:
◾ Base class for all services. When this class is extended, it's important that
you create a new thread in whic...
● Service:
⚪ It may block the Main Thread of the application.
● IntentService:
⚪ It cannot run tasks in parallel. Hence al...
Is that means that anything done by IntentService can be done with a
Service?
IntentService instead of Service?
64
Is that means that anything done by IntentService can be done with a
Service?
If yes, then why IntentService?
Why IntentSe...
Services: Bounded Service
Application Components
66
● A bound service is one that allows application components to bind to
it by calling bindService() in order to create a lo...
● The bounded service lives only to serve the application component
that is bound to it, so when there are no components b...
Example
69
Download File from the internet on demand?
Which type of service to make this task?
70
Send info about specific file update change on the system?
Which type of service to make this task?
71
execute calculations for your application in background?
Which type of service to make this task?
72
73
Ready to code!
Broadcast Receivers
Application Components
74
● A broadcast receiver is a component that responds to system-wide broadcast
announcements.
What is Broadcast Receiver?
75
● Many broadcasts originate from the system for example, a broadcast
announcing that the screen has turned off, the batter...
1. Normal broadcasts:
○ sent using sendBroadcast()
○ Are completely asynchronous.
○ All receivers of the broadcast are run...
2. Ordered broadcasts:
○ sent using sendOrderedBroadcast()
○ Delivered to one receiver at a time.
○ The order receivers ru...
● A BroadcastReceiver object is only valid for the duration of the call to
onReceive(Context, Intent). Once your code retu...
● Display a Toast when user changes wallpaper
Example
80
81
Ready to code!
Thank You
Android from A to Z
Upcoming SlideShare
Loading in …5
×

Android from A to Z

1,298 views

Published on

Android from A to Z

Published in: Technology
  • My car battery was completely dead before I used your methods! I reconditioned my dead car battery a few weeks ago with your program and it's been working perfectly since then! My car battery was completely dead before I used your methods. This just saved me hundreds of dollars on a new battery! ■■■ http://t.cn/AiFAbuQp
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Making a living taking surveys at home! I have been a stay at home mom for almost 5 years and I am so excited to be able to still stay home, take care of my children and make a living taking surveys on my own computer! It's so easy to get started and I plan to make enough money each week so that my husband can actuallly quit his second job!!! Thank you so much! ➤➤ http://ishbv.com/goldops777/pdf
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Android from A to Z

  1. 1. From A to Z Android Hazem Hagrass Senior Software Engineer hazem.hagrass@badrit.com 2
  2. 2. Agenda ● Overview ● Architecture ● Environment Setup ● Project Structure and modules ● Application Components ⚪ Intents and Filters ⚪ Activities ⚪ Services ⚪ Broadcast Receivers 3
  3. 3. Overview 4 Android: The Forgotten History
  4. 4. The Forgotten History ● Created by Andy Rubin, a free, open source mobile platform that any coder could write for and any handset maker could install. ● “There were nearly 700 million cell phones sold each year compared with fewer than 200 million PCs and the gap was widening. Increasingly, phones were the way people wanted to connect with each other and with everything else” Andy Rubin. 5
  5. 5. The Forgotten History ● He would make his money by selling support for the system security services, or email management. 6
  6. 6. The Forgotten History ● Rubin discussed the idea with Larry Page(Google's CEO), he didn’t want money from Page. He already had funding. What he wanted was Google’s imprimatur, even an email from Page would do. ● Rubin figured he could attract more VC funds with the search giant on board, possibly with a hint that Google might be interested in developing its own branded phone. He pulled out a prototype. 7
  7. 7. The Forgotten History ● When Apple introduced the iPhone in 2007, Google was already working on Android, its own smartphone operating system. Google bought Android, which was Rubin's startup, for ~$50 million in 2005. 8
  8. 8. Who is OHA? 9
  9. 9. Who is OHA? 10 ● The OHA was established on 6 November 2007 ● it is led by Google with 47 members including mobile handset makers, application developers, some mobile carriers and chip makers. ● Android, the flagship software of the alliance, is based on an open source license and has competed against mobile platforms from Apple (iOS), Microsoft, Nokia (Symbian), HP ( Palm), Samsung Electronics / Intel (Tizen, bada), and Blackberry.
  10. 10. Steve Jobs! "Everything is a f**king rip off of what we're doing," Jobs said of Android 11
  11. 11. Android powers hundreds of millions of mobile devices in more than 190 countries around the world. It's the largest installed base of any mobile platform and growing fast Android, the world's most popular mobile platform 12
  12. 12. Market Share 13
  13. 13. ● Every day more than 1 million new Android devices are activated worldwide. ● 1.5 billion downloads a month and growing. Get your apps in front of millions of users at Google's scale ● Android Nears 80% Market Share In Global Smartphone Shipments, As iOS And BlackBerry Share Slides, Per IDC Play Store 14
  14. 14. Android is open! Android is Open, it is built on the open Linux Kernel. it can be liberally extended to incorporate new cutting edge technologies as they emerge. The platform will continue to evolve as the developer community works together to build innovative mobile applications. Android was built from the ground-up to enable developers take full advantage of all a handset has to offer. It was built to be truly open. For example, an application can call upon any of the phone’s core functionality such as making calls, sending text messages, or using the camera 15
  15. 15. All applications are created equal Android does not differentiate between the phone’s core applications and third-party applications. They can all be built to have equal access to a phone’s capabilities providing users with a broad spectrum of applications and services Equality 16
  16. 16. Architecture 17 System Architecture
  17. 17. Architecture 18
  18. 18. Linux Kernel ● The Android OS is derived from Linux Kernel 2.6 and is actually created from Linux source ● This provides basic system functionality like process management, memory management, device management like camera, keypad, display etc. Also, the kernel handles all the things that Linux is really good at such as networking and a vast array of device drivers, which take the pain out of interfacing to peripheral hardware. 19
  19. 19. Libraries ● This layer holds the Android native libraries written in C/C++ and offer capabilities similar to the above layer, while sitting on top of the kernel. ● Set of libraries including open-source Web browser engine WebKit, well known library libc, SQLite database which is a useful repository for storage and sharing of application data, libraries to play and record audio and video, SSL libraries responsible for Internet security etc. 20
  20. 20. It provides a key component called Dalvik Virtual Machine which is a kind of Java Virtual Machine specially designed and optimized for Android. When we say it is for embedded devices, it means it is low on memory, comparatively slower and runs on battery power. The Dalvik VM enables every Android application to run in its own process, with its own instance of the Dalvik virtual machine. The Android runtime also provides a set of core libraries which enable Android application developers to write Android applications using standard Java programming language. Android Runtime 21
  21. 21. Application Framework It is the layer which application developers can leverage in developing Android applications. The framework offers a huge set of APIs used by developers for various standard purposes, so that they don't have to code every basic task.The framework consists of certain entities. 22
  22. 22. Your application to be installed on this layer only. Examples of such applications are Contacts Books, Browser, Games etc. Applications 23
  23. 23. Environment Setup 24 Installation
  24. 24. Now everything is easy and can be done on one click just by download Android SDK tools which contains: ● Android Studio IDE ● Android SDK tools ● Android 5.0 (Lollipop) Platform ● Android 5.0 emulator system image with Google APIs Installation 25
  25. 25. Project Structure 26 Structure and modules
  26. 26. Top Level Folders 27
  27. 27. ● Main Project: This would be entire project context (Eclipse Land: Like your workspace but limited to what's relevant to your project). Ex: HelloWorldProject if the name of the application you gave was HelloWorld ● .idea: This where project specific metadata is stored by Android Studio (AS). (Eclipse Land: project.properties file) ● gradle: This is where the gradle build system's jar wrapper i.e. this jar is how AS communicates with gradle installed in Windows (the OS in my case). ● External Libraries: This is not actually a folder but a place where Referenced Libraries (Eclipse Land: Referenced Libraries) are shown. Here's where the Targeted Platform is shown etc. Top Level Folders 28
  28. 28. ● Project Module: This is the actual project. ex: HelloWorld if your application name you gave was HelloWorld. ⚪ build: This has all the complete output of the make process i.e. classes. dex, compiled classes and resources ⚪ libs: This is the standard libs folder that you see in eclipse land too ⚪ src/AndroidManifest.xml: describes the fundamental characteristics of the app and defines each of its components. ⚪ src/java: folder contains source code that implements the functionality of your application. ⚪ src/res: folder contains various visual resources. Top Level Folders 29
  29. 29. ● Project Module: This is the actual project. ex: HelloWorld if your application name you gave was HelloWorld. ⚪ src/res/layout: contains all files that define your app's user interface. ⚪ src/res/values: contains all XML files that contain a collection of resources, such as strings and colors definitions. ⚪ src/res/drawbale-: contains drawable objects that are designed for each screens according to their density. ⚪ src/assets: This is empty. You can use it to store raw asset files. Top Level Folders 30
  30. 30. ● AndroidManifest.xml is an interface between the Android OS and your application. What is AndroidManifest.xml? 31
  31. 31. What is AndroidManifest.xml? 32
  32. 32. Application Components 33 Intents, activities, services and broadcast receivers
  33. 33. ● App components are the essential building blocks of an Android app. Each component is a different point through which the system can enter your app. ● Not all components are actual entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role each one is a unique building block that helps define your app's overall behavior. ● There are four different types of app components(Intents, activities, services and broadcast receivers). Each type serves a distinct purpose and has a distinct life cycle that defines how the component is created and destroyed. Application Components 34
  34. 34. Intents and Intent Filters Application Components 35
  35. 35. ● An Intent is a messaging object you can use to request an action from another app component or communicate between other components like activities, services or broadcast receivers. What is an Intent? 36
  36. 36. ● Although intents facilitate communication between components in several ways, there are three fundamental use-cases: ⚪ To start activity ⚪ To start service ⚪ To deliver a broadcast ● There are two types of Intents: ⚪ Explicit Intents ⚪ Implicit Intents What is an Intent? 37
  37. 37. Intents Types Explicit intents: ● Used to start a component in your own app, because you know the class name of the activity or service you want to start. ● For example, start a new activity in response to a user action or start a service to download a file in the background. 38
  38. 38. Intents Types 39 Implicit intents: ● No specific component needed , but instead declare a general action to perform, which allows a component from another app to handle it. ● For example, displaying user a location on a map can be done using simple request allowing another capable app to show a it on a map.
  39. 39. Intent contains at least one of: 40 ● Component name(optional): The name of the component to start. it's the critical piece of information that makes an intent explicit, Without a component name, the intent is implicit and the system decides which component should receive the intent based on the other intent information (such as the action, data, and category). So if you need to start a specific component in your app, you should specify the component name. ● Action(optional): A string that specifies the generic action to perform.
  40. 40. Intents Attributes 41 ● Category(optional): A string containing additional information about the kind of component that should handle the intent. Any number of category descriptions can be placed in an intent, but most intents do not require a category. Here are some common categories: ⚪ CATEGORY_BROWSABLE: The target activity allows itself to be started by a web browser to display data referenced by a link. ⚪ CATEGORY_LAUNCHER: The activity is the initial activity of a task and is listed in the system's application launcher.
  41. 41. Intents Attributes 42 ● Data(optional): The URI (a Uri object) that references the data to be acted on and/or the MIME type of that data. The type of data supplied is generally dictated by the intent's action. For example, if the action is ACTION_EDIT, the data should contain the URI of the document to edit. ● Extras(optional): Key-value pairs that carry additional information required to accomplish the requested action. Just as some actions use particular kinds of data URIs, some actions also use particular extras.
  42. 42. Intents Attributes 43 ● Flags(optional): The flags may instruct the Android system how to launch an activity (for example, which task the activity should belong to) and how to treat it after it's launched (for example, whether it belongs in the list of recent activities).
  43. 43. ● Used to register specific component as being capable for doing specific action on a set of data. ● To advertise which implicit intents your app can receive, declare one or more intent filters for each of your app components with an <intent-filter> element in your manifest file. Each intent filter specifies the type of intents it accepts based on the intent's action, data, and category. The system will deliver an implicit intent to your app component only if the intent can pass through one of your intent filters. ● An explicit intent is always delivered to its target, regardless of any intent filters the component declares What is Intent Filter? 44
  44. 44. ● Each intent filter is defined by an <intent-filter> element in the app's manifest file, nested in the corresponding app component. Example 45
  45. 45. 46 Ready to code!
  46. 46. Activities Application Components 47
  47. 47. An Activity is an application component that provides a single screen with which users can interact in order to do something. An application usually consists of multiple activities that are loosely bound to each other. Each activity can start another activity in order to perform different actions. What is Activity? 48
  48. 48. Lifecycle 49
  49. 49. Example 50
  50. 50. How to use? 51 You can start an activity by calling startActivity(), passing it an Intent that describes the activity you want to start. The intent specifies either the exact activity you want to start or describes the type of action you want to perform (and the system selects the appropriate activity for you, which can even be from a different application). An intent can also carry small amounts of data to be used by the activity that is started.
  51. 51. 52 Ready to code!
  52. 52. Services Application Components 53
  53. 53. ● A service is a component that runs in the background to perform long-running operations without needing to interact with the user. ● For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. What is Service? 54
  54. 54. ● A service can essentially take two forms: a. Started(Unbounded): A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. When the operation is done, the service should stop itself. Services Forms 55
  55. 55. ● A service can essentially take two forms: b. Bound: A service is "bound" when an application component binds to it by calling bindService(). A bound service runs only as long as the application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed. Services Forms(cont’) 56
  56. 56. ● A service runs in the main thread of its hosting process. ● The service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread. Lifecycle 57
  57. 57. Lifecycle(cont’) 58
  58. 58. ● If the service is bound to an activity that has user focus, then it's less likely to be killed, and if the service is declared to run in the foreground, then it will almost never be killed. Otherwise, if the service was started and is long-running, then the system will lower its position in the list of background tasks over time and the service will become highly susceptible to killing. ● if your service is started, then you must design it to gracefully handle restarts by the system. If the system kills your service, it restarts it as soon as resources become available again. Lifecycle(cont’) 59
  59. 59. Example 60
  60. 60. Services: Started Service Application Components 61
  61. 61. ⚪ Service: ◾ Base class for all services. When this class is extended, it's important that you create a new thread in which to do all the service's work. ⚪ IntentService: ◾ Subclass of Service that uses a worker thread to handle all start requests, one at a time. This is the best option if you don't require that your service handle multiple requests simultaneously. All you need to do is implement onHandleIntent(), which receives the intent for each start request so you can do the background work. Started Service Forms 62
  62. 62. ● Service: ⚪ It may block the Main Thread of the application. ● IntentService: ⚪ It cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially. ⚪ It must be triggered from Main Thread. Started Service Limitations 63
  63. 63. Is that means that anything done by IntentService can be done with a Service? IntentService instead of Service? 64
  64. 64. Is that means that anything done by IntentService can be done with a Service? If yes, then why IntentService? Why IntentService? 65
  65. 65. Services: Bounded Service Application Components 66
  66. 66. ● A bound service is one that allows application components to bind to it by calling bindService() in order to create a long-standing connection. ● You should create a bound service when you want to interact with the service from activities and other components in your application or to expose some of your application's functionality to other applications ● To create a bound service, you must implement the onBind() callback method to return an IBinder that defines the interface for communication with the service. Other application components can then call bindService() to retrieve the interface and begin calling methods on the service. What is Bounded Service? 67
  67. 67. ● The bounded service lives only to serve the application component that is bound to it, so when there are no components bound to the service, the system destroys it. What is Bounded Service? 68
  68. 68. Example 69
  69. 69. Download File from the internet on demand? Which type of service to make this task? 70
  70. 70. Send info about specific file update change on the system? Which type of service to make this task? 71
  71. 71. execute calculations for your application in background? Which type of service to make this task? 72
  72. 72. 73 Ready to code!
  73. 73. Broadcast Receivers Application Components 74
  74. 74. ● A broadcast receiver is a component that responds to system-wide broadcast announcements. What is Broadcast Receiver? 75
  75. 75. ● Many broadcasts originate from the system for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. ● Apps can also initiate broadcasts for example, to let other apps know that some data has been downloaded to the device and is available for them to use. ● broadcast receivers don't display a user interface. ● They may create a status bar notification to alert the user when a broadcast event occurs. ● Broadcast receiver is just a "gateway" to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event. What is Broadcast Receiver? 76
  76. 76. 1. Normal broadcasts: ○ sent using sendBroadcast() ○ Are completely asynchronous. ○ All receivers of the broadcast are run in an undefined order, often at the same time. ○ This basically means that one receiver can not interfere in any way with what other receivers will do neither can it prevent other receivers from being executed. One example of such broadcast is the ACTION_BATTERY_LOW one. Broadcast Receiver Types 77
  77. 77. 2. Ordered broadcasts: ○ sent using sendOrderedBroadcast() ○ Delivered to one receiver at a time. ○ The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order ○ ACTION_NEW_OUTGOING_CALL is a live example. This broadcast is sent whenever the user tries to initiate a phone call. There are several reasons that one would want to be notified about this,as ■ To be able to reject an outgoing call. ■ To be able to rewrite the number before it is dialed. Broadcast Receiver Types 78
  78. 78. ● A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active. ● This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes. Lifecycle 79
  79. 79. ● Display a Toast when user changes wallpaper Example 80
  80. 80. 81 Ready to code!
  81. 81. Thank You

×