82

Android from A to Z

Embed Size (px)

Citation preview

Page 1: Android from A to Z
Page 2: Android from A to Z

From A to Z

Android

Hazem HagrassSenior Software [email protected] 2

Page 3: Android from A to Z

Agenda

● Overview● Architecture● Environment Setup● Project Structure and modules● Application Components

⚪ Intents and Filters⚪ Activities⚪ Services⚪ Broadcast Receivers

3

Page 4: Android from A to Z

Overview

4

Android: The Forgotten History

Page 5: 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.

5

Page 6: Android from A to Z

The Forgotten History

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

6

Page 7: Android from A to Z

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

Page 8: 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.

8

Page 10: Android from A to Z

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.

Page 11: Android from A to Z

Steve Jobs!

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

11

Page 12: 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

12

Page 13: Android from A to Z

Market Share

13

Page 14: 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

14

Page 15: 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

15

Page 16: 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

16

Page 17: Android from A to Z

Architecture

17

System Architecture

Page 18: Android from A to Z

Architecture

18

Page 19: 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.

19

Page 20: 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.

20

Page 21: 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

21

Page 22: 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.

22

Page 23: 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

23

Page 24: Android from A to Z

Environment Setup

24

Installation

Page 25: Android from A to Z

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

Page 26: Android from A to Z

Project Structure

26

Structure and modules

Page 27: Android from A to Z

Top Level Folders

27

Page 28: 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

28

Page 29: 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.⚪ src/java: folder contains source code that implements the functionality

of your application.⚪ src/res: folder contains various visual resources.

Top Level Folders

29

Page 30: Android from A to Z

● 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

Page 31: Android from A to Z

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

What is AndroidManifest.xml?

31

Page 32: Android from A to Z

What is AndroidManifest.xml?

32

Page 33: Android from A to Z

Application Components

33

Intents, activities, services and broadcast receivers

Page 34: 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

34

Page 35: Android from A to Z

Intents and Intent Filters

Application Components

35

Page 36: 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?

36

Page 37: Android from A to Z

● 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

Page 38: 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.

38

Page 39: Android from A to Z

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.

Page 40: Android from A to Z

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.

Page 41: Android from A to Z

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.

Page 42: Android from A to Z

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.

Page 43: Android from A to Z

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

Page 44: 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?

44

Page 45: 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

45

Page 46: Android from A to Z

46

Ready to code!

Page 47: Android from A to Z

Activities

Application Components

47

Page 48: 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?

48

Page 49: Android from A to Z

Lifecycle

49

Page 50: Android from A to Z

Example

50

Page 51: Android from A to Z

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.

Page 52: Android from A to Z

52

Ready to code!

Page 53: Android from A to Z

Services

Application Components

53

Page 54: 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?

54

Page 55: 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

55

Page 56: 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’)

56

Page 57: 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

57

Page 58: Android from A to Z

Lifecycle(cont’)

58

Page 59: 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’)

59

Page 60: Android from A to Z

Example

60

Page 61: Android from A to Z

Services: Started Service

Application Components

61

Page 62: 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

62

Page 63: 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

63

Page 64: Android from A to Z

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

IntentService instead of Service?

64

Page 65: 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?

65

Page 66: Android from A to Z

Services: Bounded Service

Application Components

66

Page 67: 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?

67

Page 68: 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?

68

Page 69: Android from A to Z

Example

69

Page 70: Android from A to Z

Download File from the internet on demand?

Which type of service to make this task?

70

Page 71: Android from A to Z

Send info about specific file update change on the system?

Which type of service to make this task?

71

Page 72: Android from A to Z

execute calculations for your application in background?

Which type of service to make this task?

72

Page 73: Android from A to Z

73

Ready to code!

Page 74: Android from A to Z

Broadcast Receivers

Application Components

74

Page 75: Android from A to Z

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

What is Broadcast Receiver?

75

Page 76: 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?

76

Page 77: 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

77

Page 78: 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

78

Page 79: 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

79

Page 80: Android from A to Z

● Display a Toast when user changes wallpaper

Example

80

Page 81: Android from A to Z

81

Ready to code!

Page 82: Android from A to Z

Thank You