Lesson 3 Android Components

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Introduction to Android

• Android is a software package and linux based operating system for mobile devices such as
tablet computers and smart phones released first version in 2007.
• It is developed by Google and later the OHA (Open Handset Alliance).
• Android is a complete set of software for mobile devices such as tablet computers, notebooks,
smart phones, electronic book readers, set-top boxes etc.
• It contains a linux-based Operating System, middleware and key mobile applications. It can
be thought of as a mobile operating system. But it is not limited to mobile only. It is currently
used in various devices such as mobiles, tablets, televisions etc.
• Java language is mainly used to write the android code even though other languages can be
used. The goal of android project is to create a successful real-world product that improves the
mobile experience for end users. There are many code names of android such as Lollipop,
Kitkat, Jelly Bean, Ice cream Sandwich, Froyo, Ecliar, Donut etc
Features of Android
1) It is open-source.
2) Anyone can customize the Android Platform.
3) There are a lot of mobile applications that can be chosen by the consumer.
4) It provides many interesting features like weather details, opening screen, live
RSS (Really Simple Syndication) feeds etc. It provides support for messaging
services(SMS and MMS), web browser, storage (SQLite), connectivity (GSM,
CDMA, Blue Tooth, Wi-Fi etc.), media, handset layout etc.
Android IDEs
There are so many sophisticated Technologies are available to develop android
applications, the familiar technologies, which are predominantly using tools as
follows
 Android Studio
 Eclipse IDE
 Intellij
 Visual Studio (c++)
 etc
Android Architecture
Android operating system is a stack of software components which is roughly divided
into five sections and four main layers as shown below in the architecture diagram.
Linux kernel
 This provides a level of abstraction between the device hardware and it contains all the
essential hardware drivers like camera, keypad, display etc.
 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

Libraries
• On top of Linux kernel there is a 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.
Android Libraries
• A summary of some key core Android libraries available
to the Android developer is as follows
Android Runtime
• Having covered the Java-based core libraries in the Android runtime,
it is now time to turn our attention to the C/C++ based libraries
contained in this layer of the Android software stack.
• This section provides a key component called Dalvik Virtual
Machine (DVM) which is a kind of Java Virtual Machine specially
designed and optimized for Android
• The Dalvik VM makes use of Linux core features like memory
management and multithreading, which is intrinsic in the Java
language
• 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.
Dalvik Virtual Machine | DVM
• Modern JVM is high performance and provides
excellent memory management. But it needs to be
optimized for low-powered handheld devices as
well.
• The Dalvik Virtual Machine (DVM) is an android
virtual machine optimized for mobile devices. It
optimizes the virtual machine for memory, battery
life and performance.
• The Dex compiler converts the class files into the
.dex file that run on the Dalvik VM.
• Multiple class files are converted into one dex file.
Let's see the compiling and packaging process
from the source file:
The javac tool compiles the java source file into the
class file. The dx tool takes all the class files of your
application and generates a single .dex file.
It is a platform-specific tool. The Android Assets
Packaging Tool (aapt) handles the packaging
process.
Application Framework
• The Application Framework layer provides many higher-
level services to applications in the form of Java classes.
• Application developers are allowed to make use of these
services in their applications
Applications
• Android application at the top layer. You will write your
application to be installed on this layer only. Examples of
such applications are Contacts Books, Browser, Games
etc.
Application Component
• essential building blocks of an Android application
• These components are loosely coupled by the application
manifest file AndroidManifest.xml that describes each
component of the application and how they interact.

There are following four main components that can be used within an Android application:
Components Description
Activities They dictate the UI and handle the user interaction to the smartphone screen

Services They handle background processing associated with an application.


Broadcast Receivers They handle communication between Android OS and applications.
Content Providers They handle data and database management issues.
Activities
 An activity represents a single screen with a user interface. For example,
 email application might have one activity that shows a list of new emails,
 another activity to compose an email,
 another activity for reading emails.
 If an application has more than one activity, then one of them should be marked as the
activity that is presented when the application is launched.
 An activity is implemented as a subclass of Activity class as follows:

public class MainActivity extends Activity { }

Services
A service is a component that runs in the background to perform long-running operations.
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. A service is implemented as a subclass of Service class as follows:
public class MyService extends Service {
Broadcast Receivers
Broadcast Receivers simply respond to broadcast messages from other applications or from the
system. For example, applications can also initiate broadcasts to let other applications know
that some data has been downloaded to the device and is available for them to use, so this is
broadcast receiver who will intercept this communication and will initiate appropriate action.

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and each


message is broadcasted as an Intent object.
public class MyReceiver extends BroadcastReceiver { }

Content Providers
A content provider component supplies data from one application to others on request. Such
requests are handled by the methods of the ContentResolver class. The data may be stored in
the file system, the database or somewhere else entirely. A content provider is implemented as
a subclass of ContentProvider class and must implement a standard set of APIs that enable
other applications to perform transactions.
public class MyContentProvider extends ContentProvider { }
Other Components
Android Programming
Anatomy of Android Application
Before you run your app, you should be aware of a few directories and files in the Android
project:

1. src This contains the .java source files for your project. By default, it includes an
MainActivity.javasource file having an activity class that runs when your app is launched using
the app icon.
2 . gen This contains the .R file, a compiler-generated file that references all the resources found
in your project. You should not modify this file.
3. bin This folder contains the Android package files .apk built by the ADT during the build
process and everything
4. res/drawable-hdpi This is a directory for drawable objects that are designed for high-density
screens.
5. res/layout This is a directory for files that define your app's user interface.
6 . res/values This is a directory for other various XML files that contain a collection of resources,
such as strings and colors definitions.
7. AndroidManifest.xml This is the manifest file which describes the fundamental characteristics
of the app and defines each of its components.
The Main Activity File
The main activity code is a Java file MainActivity.java. This is the actual application file
which ultimately gets converted to a Dalvik executable and runs your application. Following is
the default code generated by the application wizard for Hello World! application:

onCreate( ) method is one of many


methods that are fired when an
activity is loaded

refers to the
activity_main.xml file
located in the res/layout
folder
The Manifest File
Every component developed as a part of application, must declare all its components in a
manifest file called AndroidManifest.xml
This file works as an interface between Android OS and your application, so if you do not
declare your component in this file, then it will not be considered by the OS.
For example, a default manifest file will look like as following file:
 This tags<application>...</application> enclosed the components related to the application.
Attributeandroid:icon will point to the application icon available under res/drawable-hdpi.
The application uses the image named ic_launcher.png located in the drawable folders
 The <activity> tag is used to specify an activity and android:name attribute specifies the
fully qualified class name of the Activity subclass and the android:label attributes specifies a
string to use as the label for the activity. You can specify multiple activities using <activity>
tags.
 The action for the intent filter is named android.intent.action.MAIN to indicate that this
activity serves as the entry point for the application. The category for the intent-filter is
namedandroid.intent.category.LAUNCHER to indicate that the application can be launched
from the device's launcher icon.
The @string refers to the strings.xml file explained in next slide. Hence, @string/app_name
refers to the app_name string defined in the strings.xml file, which is “HelloWorld "
Tags Used in manifest file
• Tags in manifest file to specify different Android
application components:
The Strings File
 The strings.xml file is located in the res/values folder and it contains all the text
that your application uses.
 For example, the names of buttons, labels, default text, and similar types of strings
go into this file.
 This file is responsible for their textual content. For example, a default strings file
will look like as following file:
The R File

• The gen/com.example.helloworld/R.java file is the glue


between the activity Java files likeMainActivity.java and the
resources like strings.xml. It is an automatically generated
file and you should not modify the content of the R.java file.
The Layout File
 The activity_main.xml is a layout file available in res/layout directory, that is
referenced by your application when building its interface.
 You will modify this file very frequently to change the layout of your application.
 Check layout of "Hello World!" application,

The TextView is an Android control used to build the GUI and it have various attribuites
like android:layout_width,android:layout_height etc which are being used to set its width
and height etc. The @string refers to the strings.xml file located in the res/values folder.
Hence, @string/hello_world refers to the hello string defined in the strings.xml fi le,
which is "Hello World!".

You might also like