Top Banner
Android From A to Z Hazem Hagrass Senior Software Engineer [email protected] 1
73

Android From A to Z

Dec 25, 2015

Download

Documents

Android From A to Z
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Android From A to Z

AndroidFrom A to Z

Hazem HagrassSenior Software [email protected] 1

Page 2: Android From A to Z

Agenda

● Overview● Architecture● Project Structure● Application Components

○ Intents and Filters○ Activities○ Services○ Broadcast Receivers

2

Page 3: Android From A to Z

Overview

3

Android: The Forgotten History

Page 4: Android From A to Z

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.

4

Page 5: Android From A to Z

The Forgotten History

● He would make his money by selling support for the system security services, or email management.

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

5

Page 6: Android From A to Z

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.

6

Page 7: Android From A to Z

Who is OHA?

7

The OHA was established on 6 November 2007, 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.

Page 8: Android From A to Z

Steve Jobs!

"Everything is a f**king rip off of what we're doing," Jobs said of Android

8

Page 9: Android From A to Z

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

9

Page 10: Android From A to Z

Market Share

10

Page 11: Android From A to Z

● 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

11

Page 12: Android From A to Z

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

12

Page 13: Android From A to Z

All applications are created equalAndroid 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

13

Page 14: Android From A to Z

Architecture

14

System Architecture

Page 15: Android From A to Z

Architecture

15

Page 16: Android From A to Z

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.

16

Page 17: Android From A to Z

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.

17

Page 18: Android From A to Z

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

18

Page 19: Android From A to Z

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.

19

Page 20: Android From A to Z

Your application to be installed on this layer only. Examples of such applications are Contacts Books, Browser, Games etc.

Applications

20

Page 21: Android From A to Z

Project Structure

21

Structure and modules

Page 22: Android From A to Z

Top Level Folders

22

Page 23: Android From A to Z

● 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

23

Page 24: Android From A to Z

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

■ java: folder contains source code that implements the functionality of your application.

■ res: folder contains various visual resources.

Top Level Folders

24

Page 25: Android From A to Z

● AndroidManifest.xml is an interface between the Android OS and your application.

What is AndroidManifest.xml?

25

Page 26: Android From A to Z

Application Components

26

Intents, activities, services and broadcast receivers

Page 27: Android From A to Z

● 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

27

Page 28: Android From A to Z

Intents and Intent Filters

Application Components

28

Page 29: Android From A to Z

● 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?

29

Page 30: Android From A to Z

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.

30

Page 31: Android From A to Z

Intents Types

31

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.

Page 32: Android From A to Z

Intent contains at least one of:

32

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

Page 33: Android From A to Z

Intents Attributes

33

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

Page 34: Android From A to Z

Intents Attributes

34

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

Page 35: Android From A to Z

● 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?

35

Page 36: Android From A to Z

● Each intent filter is defined by an <intent-filter> element in the app's manifest file, nested in the corresponding app component.

Example

36

Page 37: Android From A to Z

37Ready to code!

Page 38: Android From A to Z

Activities

Application Components

38

Page 39: Android From A to Z

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?

39

Page 40: Android From A to Z

Lifecycle

40

Page 41: Android From A to Z

Example

41

Page 42: Android From A to Z

How to use?

42

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.

Page 43: Android From A to Z

43Ready to code!

Page 44: Android From A to Z

Services

Application Components

44

Page 45: Android From A to Z

● 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?

45

Page 46: Android From A to Z

● 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

46

Page 47: Android From A to Z

● 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’)

47

Page 48: Android From A to Z

● 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

48

Page 49: Android From A to Z

Lifecycle(cont’)

49

Page 50: Android From A to Z

● 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’)

50

Page 51: Android From A to Z

Example

51

Page 52: Android From A to Z

Services: Started Service

Application Components

52

Page 53: Android From A to Z

○ 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

53

Page 54: Android From A to Z

● 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

54

Page 55: Android From A to Z

Is that means that anything done by IntentService can be done with a Service?

IntentService instead of Service?

55

Page 56: Android From A to Z

Is that means that anything done by IntentService can be done with a Service?

If yes, then why IntentService?

Why IntentService?

56

Page 57: Android From A to Z

Services: Bounded Service

Application Components

57

Page 58: Android From A to Z

● 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?

58

Page 59: Android From A to Z

● 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?

59

Page 60: Android From A to Z

Example

60

Page 61: Android From A to Z

Download File from the internet on demand?

Which type of service to make this task?

61

Page 62: Android From A to Z

Send info about specific file update change on the system?

Which type of service to make this task?

62

Page 63: Android From A to Z

execute calculations for your application in background?

Which type of service to make this task?

63

Page 64: Android From A to Z

Broadcast Receivers

Application Components

64

Page 65: Android From A to Z

65Ready to code!

Page 66: Android From A to Z

● A broadcast receiver is a component that responds to system-wide broadcast announcements.

What is Broadcast Receiver?

66

Page 67: Android From A to Z

● 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?

67

Page 68: Android From A to Z

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

68

Page 69: Android From A to Z

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

69

Page 70: Android From A to Z

● 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

70

Page 71: Android From A to Z

● Display a Toast when user changes wallpaper

Example

71

Page 72: Android From A to Z

72Ready to code!

Page 73: Android From A to Z

Thank You

73