Unit 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 46

Mobile Application Development

By : Dr. Anjali Gautam


Android Programming
• Books to follow
– Head first android development- Dawn Griffiths and
David Griffiths(Publisher:O'Reilly)
– Android application development for java programmers
by James C. Sheusi(Publisher :Cengage Learning ,2013)
– Java: The Complete Reference-Herbert Schildt,5th
Edition
– https://developer.android.com/guide/components/intents
-filters.html
– https://developer.android.com/guide/components/activiti
es.html
– https://developer.android.com/training/multiscreen/scree
nsizes.html
History of Android
• Android has become the most popular OS on mobile
phones in the United States.
• Google acquired Android, Inc., a 22-month-old start-up,
which signaled Google’s push into the wireless market.
• In 2007, Google and several other industry giants
(Motorola, Toshiba, Texas Instruments, and T-Mobile)
formed the Open Handset Alliance.
• The alliance members released a significant amount of
intellectual property into open source and released the
Android platform.
History of Android
• In September 2008, T-Mobile released the G1, the first smart
phone based on Android.
• It ran Android 1.0, the world’s first open-source mobile OS.
• April 2009, Android 1.6 added Google Maps.
• Later, Android 2.2, nicknamed Froyo was released.
• Froyo offered an OS tune-up for speed, USB tethering for WiFi
hot spots, and support for Adobe Flash 10.1 for watching
videos on the built-in browser.
• 2010, Motorola released the Backflip and Droid X, and T-
Mobile released the G2.
• February 2011, Android released 3.0, the made-for-tablet
installment.
Features of Android
• Linux OS based
• Designed for general-purpose handheld
computing platform
• Android libraries control telephony, video,
graphics, and the user interface
• Android OS is a multiuser system - each
application is treated as a different user with a
unique user ID
Features of Android
• Android software development kit (SDK)
supports most of the Java Standard Edition.
• Android replaces the Java abstract windowing
toolkit (AWT) and Swing packages with its own
user interface (UI) framework.
• Android supplies its own optimized JVM called
the Dalvik virtual machine.
Android Architecture
Android operating system is a stack of software
components which is roughly divided into five
sections and four main layers:
• linux kernel
• native libraries (middleware),
• Android Runtime
• Application Framework
• Applications
Android Architecture
Linux kernel
• At the bottom of the layers is Linux - Linux 2.6
with approximately 115 patches. 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.
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 Runtime
• This is the third section of the architecture and
available on the second layer from the
bottom. This section provides a key
component called Dalvik Virtual Machine
which is a kind of Java Virtual Machine
specially designed and optimized for Android.
Android Runtime
• The Dalvik VM makes use of Linux core
features like memory management and multi-
threading, 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.
Android Runtime
• The Android runtime also provides a set of
core libraries which enable Android
application developers to write Android
applications using standard Java programming
language.
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
• You will find all the 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.
Android Application Components
• Application components are the 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 four different types of app components:
• Activities
• Services
• Broadcast receivers
• Content providers
Activities
• An activity is the entry point for interacting with the
user. It represents a single screen with a user
interface.
• For example, an email app might have one activity
that shows a list of new emails, another activity to
compose an email, and another activity for reading
emails.
• During application development, an activity is written
as a single Java class, with the application’s main class
extending the Android development kit’s Activity
class.
Activities
• Activities are independent of one another,
they are allowed to work together with one
activity initiating another. 
• Each activity is associated with one screen
that is displayed.
• public class MainActivity extends Activity { }
subclass of activity class.
Service
• A process that runs in the background to perform
long-term operations or work for remote
processes.
• 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.
• The developer creates a service as a subclass of the
Android Service class. public class MyService
extends Service { }.
Content provider
• A content provider manages a shared set of app data that
you can store in the file system, in a SQLite database, on
the web, or on any other persistent storage location that
your app can access.
• Manages persistent data on the device or external sources
such as the web or cloud, or any other system the
application has access to.
• A content provider is implemented as a subclass
of Content Provider and must implement a standard set of
APIs that enable other apps to perform transactions.
• public class MyContentProvider extends ContentProvider {
public void onCreate(){} }
Broadcast receiver
• A broadcast receiver is a component that enables
the system to deliver events to the app outside of
a regular user flow, allowing the app to respond to
system-wide broadcast announcements.
• for example, an app can schedule an alarm to post
a notification to tell the user about an upcoming
event... and by delivering that alarm to a Broadcast
Receiver of the app, there is no need for the app
to remain running until the alarm goes off.
Broadcast receiver
• Capable of handling both the app and the
system broadcast.
• Although broadcast receivers don't display a
user interface, they may create a status bar
notification to alert the user when a broadcast
event occurs
Additional Components
Anatomy of Android Application
Folder Name & Description
• src -This contains the .java source files for your
project. By default, it includes anMainActivity.java
source file having an activity class that runs when
your app is launched using the app icon.
• 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.
Folder Name & Description
• bin -This folder contains the Android package
files .apk built by the ADT during the build
process and everything else needed to run an
Android application.

• res/drawable-hdpi -This is a directory for


drawable objects that are designed for high-
density screens.
Folder Name & Description
• res/layout -This is a directory for files that define
your app's user interface.

• res/values -This is a directory for other various XML


files that contain a collection of resources, such as
strings and colors definitions.

• 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:
The Main Activity File
The Main Activity File
• R.layout.activity_main refers to the
activity_main.xml file located in the res/layout
folder. The onCreate() method is one of many
methods that are fired when an activity is
loaded.
The Manifest File
• Whatever component you develop as a part of
your application, you must declare all its
components in a manifest file called
AndroidManifest.xml which resides at the root
of the application project directory.
• 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
The Manifest File
The Manifest File
• <application>...</application> tags enclosed the
components related to the application. Attribute
android:icon will point to the application icon available
underres/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 Manifest File
• 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 named
android.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 below.
Hence, @string/app_name refers to the app_name string
defined in the strings.xml file, which is "HelloWorld". Similar
way, other strings get populated in the application.
The Manifest File
• List of tags which you will use in your manifest
file to specify different Android application
components:
• <activity>elements for activities
• <service> elements for services
• <receiver> elements for broadcast receivers
• <provider> elements for content providers
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.
The Strings File
A default string file will look like as following file:
Setting up environment
• The Android SDK - The Android Software
Development Kit contains the libraries and
tools you need to develop Android apps.
• Java – java version 7 or above.
• Open a new project, name it.
Building an App
• An activity is a single, defined thing that your user
can do. You might have an activity to compose an
email, take a photo, or find a contact. Activities are
usually associated with one screen, and they’re
written in Java.
• A layout describes the appearance of the screen.
Layouts are written as XML files and they tell
Android how the different screen elements are
arranged.
• Layouts define how the user interface is presented.
• Activities define actions.
Building an app
1. Create an activity – choose any of the activity
from varied option.
• Create an app with a basic activity and layout, so
choose the Blank Activity option.
2. Configure the activity -Enter an activity name of
“MainActivity”, and a layout name of
“activity_main”.
• MainActivity wil be a class and activity_main is
an xml file.
Building an app
• The Android Studio wizard created a project for your
app, configured to your specifications.
– You defined which versions of Android the app should be
compatible with, and the wizard created all of the files and
folders needed for a basic valid app.
• It created a basic activity and layout with template
code.
– The template code includes layout XML and activity Java code,
with sample “Hello world!” text in the layout. You can change
this code.
• The project is able to execute the app.
Code Editor
• Launched when you open a file to edit the
code.
• Open activity_main.xml- a layout file.
• Has two views – Design and Text.
• Complete coding your app.
Run the App
• use the Android emulator that’s built into the Android
SDK.
• It looks just like a phone running on your computer.
• The emulator is an application that re-creates the exact
hardware environment of an Android device: from its
CPU and memory, through to the sound chips and the
video display. The emulator is built on an existing
emulator called QEMU.
• Creating an Android Virtual Device.
• Running the app.
Run the App
• Android Studio launches the emulator, loads
the AVD, and installs the app.
• When the app gets launched, an activity object
is created from MainActivity.java.
• The activity specifies that it uses the layout
activity_main.xml.
• The activity tells Android to display the layout
on the screen. The text “Hello world!” gets
displayed.

You might also like