Вы находитесь на странице: 1из 12

Android Tutorial

What is Android?
Operating Systems have developed a lot in last 15 years. Starting from black and white phones to
recent smart phones or mini computers, mobile OS has come far away. Especially for smart
phones, Mobile OS has greatly evolved from Palm OS in 1996 to Windows pocket PC in 2000
then to Blackberry OS and Android.
One of the most widely used mobile OS these days is ANDROID. Android is a software bunch
comprising not only operating system but also middleware and key applications. Android Inc
was founded in Palo Alto of California, U.S. by Andy Rubin, Rich miner, Nick sears and Chris
White in 2003. Later Android Inc. was acquired by Google in 2005. After original release there
have been number of updates in the original version of Android.

Features of Android:
Feature

Description

Beautiful UI

Android OS basic screen provides a


beautiful and intuitive user interface.

Connectivity

GSM/EDGE, IDEN, CDMA, EV-DO,


UMTS, Bluetooth, Wi-Fi, LTE, NFC and
WiMAX.

Storage

SQLite, a lightweight relational database,


is used for data storage purposes.

Media support

H.263, H.264, MPEG-4 SP, AMR, AMRWB, AAC, HE-AAC, AAC 5.1, MP3,
MIDI, Ogg Vorbis, WAV, JPEG, PNG, GIF,
and BMP

Messaging

SMS and MMS

Web browser

Based on the open-source WebKit layout


engine, coupled with Chrome's V8
JavaScript engine supporting HTML5 and
CSS3.

Multi-touch

Android has native support for multi-touch


which was initially made available in
handsets such as the HTC Hero.

Multi-tasking

User can jump from one task to another and


same time various application can run
simultaneously.

Resizable widgets

Widgets are resizable, so users can expand


them to show more content or shrink them to
save space.

Multi-Language

Supports single direction and bi-directional


text.

GCM

Google Cloud Messaging (GCM) is a


service that lets developers send short
message data to their users on Android
devices, without needing a proprietary sync
solution.

Wi-Fi Direct

A technology that lets apps discover and pair


directly, over a high-bandwidth peer-to-peer
connection.

Android Beam

A popular NFC-based technology that lets

users instantly share, just by touching two


NFC-enabled phones together.

Android Applications:
Android applications are usually developed in the Java language using the Android Software
Development Kit. Once developed, Android applications can be packaged easily and sold out
either through a store such as Google Play or the Amazon Appstore. 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. Every day more than 1 million new Android
devices are activated worldwide. This tutorial has been written with an aim to teach you how to
develop and package Android application. We will start from environment setup for Android
application programming and then drill down to look into various aspects of Android
applications.

Android OS Architectur
Linux kernel
1. Libraries
2. Android Runtime
3. Application framework
4. Applications
Android OS Architecture

Linux kernel
1. Bottom layer of android operating system is Linux kernel.
2. Android is built on top of Linux 2.6 Kernel and few architectural changes made by Google.
3. Linux Kernel provides the basic system functionality such as process management, memory
management and device management
4. Linux kernel also provides array of device drivers which make our task easier while
interfacing the android with peripheral devices.
Libraries
1. On the top of Linux Kernel another layer called libraries is present.
2. It provides the different libraries useful for well functioning of android operating system.
3. Libraries are java libraries build specific for android operating system.
Some of the libraries are listed below

Libraries

Explanation

SQLite

It is used to access data published by content


providers and includes SQLite database
management classes
It is used to provide internet security

SSL

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

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 following four main components that
can be used within an Android
application:
Components
Activities
Services
Broadcast Receivers

They they dictate the UI and handle the


user interaction to the smartphone screen
They handle background processing
associated with an application.
They handle communication between

Content Providers

Android OS and applications.


They handle data and database
management issues.

Activities:
An activity in Android represents a single screen with user interface. An android application
typically consists of several activities. An activity interacts with the user to do one thing, only
ONE, such as Unlock screen, dial a phone, view home etc. An application consists of multiple
activities that are loosely bound together. But only one activity can be specified as main activity
which is displayed while launching the application. Every time an activity starts previous activity
is stopped but the data is preserved.

Content Providers:
Android is embedded with SQLite Database where all your data gets stored. Content providers in
Android manage data that is being shared by more than one application. Consider a case where
your Contacts are stored in centralized repository, so here many applications may require access
to it or may even require modifying it. In such cases these applications need to query the
centralized repository through content providers, so an application with proper permissions can
read or modify the Contacts based on permissions. Content provider is concept evolved to
manage common data based on permissions. This is a critical concept that has led to develop inhouse android applications in a better way.

Broadcast Receivers:
Broadcast receiver is also a component where you can register for system or application events.
Once registered you will be notified about the events. Broadcast originates from the system as
well as applications. Instance for broadcast originating from the system is low battery
notification. Application level is, like when you download an mp3 file, Mp3 player gets notified
about it, and gets added to player list. For this action to take place, mp3 player has to register for
this event. To get detailed information on Broadcast receiver in Android.

Activity life cycle


An activity can be in different states depending how it is interacting with the user. These
states are described by the following table.
Activity state

State

Description

Running

Activity is visible and interacts with the user.

Paused

Activity is still visible but partially obscured, instance is running but might be killed by the system.

Stopped

Activity is not visible, instance is running but might be killed by the system.

Killed

Activity has been terminated by the system of by a call to itsfinish() method.

The live cycle methods


The Android system defines a life-cycle for activities via predefined life-cycle methods. The
most important methods are:
Method

Purpose

onCreate()

Called then the activity is created. Used to initialize the activity, for example create the user interface.

onResume()

Called if the activity get visible again and the user starts interacting with the activity again. Used to initialize fields,
register listeners, bind to services, etc.

onPause()

Called once another activity gets into the foreground. Always called before the activity is not visible anymore. Used to
release resources or save application data. For example you unregister listeners, intent receivers, unbind from services
or remove system service listeners.

onStop()

Called once the activity is no longer visible. Time or CPU intensive shut-down operations, such as writing information
to a database should be down in theonStop() method. This method is guaranteed to be called as of API 11.

The life cycle of an activity with its most important methods is displayed in the following
diagram.

Example:
Public classMainActivity extends ActionBarActivity{
@Override
Protected voidnCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//android.os.Debug.startMethodTracing();
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.v("Debug","MainActivity onDestroy invoked");
}
@Override

public void onPause(){


super.onPause();
Log.v("Debug","MainActivity OnPause invoked");
}
@Override
public void onResume(){
super.onResume();
Log.v("Debug","MainActivity onResume invoked");
}
@Override
protected void onStop(){
super.onStop();
Log.v("Debug","MainActivity onStop invoked");
}
@Override
protected voidonRestart(){
super.onRestart();
Log.v("Debug","MainActivity onRestart invoked");
}

Android Layouts
I will be discussing about the different view layouts in an android mobile application. The six
different layouts are

1. Linear Layout
2. Relative Layout
3. Table Layout
4. Grid View
5. Tab Layout
6. List View

1. Linear Layout
In a linear layout, like the name suggests, all the elements are displayed in a linear fashion(below
is an example of the linear layouts), either Hori zon tally or Vertically and this behavior is
set in and roid :orientation which is an attribute of the node LinearLayout.
Example of Vertical layout snippet
<LinearLayout android:orientation="vertical"> .... </LinearLayout>
Example of Horizontal layout snippet

<LinearLayout android:orientation="horizontal"> .... </LinearLayout>


1. Create a new project File -> New -> Android Project
2. In Package Explorer right click on res/lay ou t folder and create a new Android XML File
and name it as you wish. I am naming it as linear_layou t.xm l
res/layout -> Right Click -> New -> Android XML File
3. Now open newly created xml file (in my case lin ear_lay out.xm l ) and type the
following code.

Relative Layout
In a relative layout every element arranges itself relative to other elements or a parent element.
As an example, lets consider the layout defined below. The Can cel button is placed
relatively, to the right of the Login button p arallely . Here is the code snippet that
achieves the mentioned alignment (Right of Login button parallely)

Intents
An Android Intent is an abstract description of an operation to be performed. It can be used
with startActivity to launch an Activity, broadcastIntent to send it to any interested
BroadcastReceiver
components,
and startService(Intent)or bindService(Intent,
ServiceConnection, int) to communicate with a background Service.

Types of Intents
Explicit Intents
Explicit intent going to be connected internal world of application,suppose if you wants to
connect one activity to another activity, we can do this quote by explicit intent, below image is
connecting first activity to second activity by clicking button

// Explicit Intent by specifying its class name


Intent i = new Intent(FirstActivity.this, SecondActivity.class);

// Starts TargetActivity
startActivity(i);

Implicit Intents
These intents do not name a target and the field for the component name is left blank. Implicit
intents are often used to activate components in other applications. For example
Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
email.putExtra(Intent.EXTRA_EMAIL, recipients);

email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
email.putExtra(Intent.EXTRA_TEXT, body.getText().toString());
startActivity(Intent.createChooser(email, "Choose an email client from..."));

Вам также может понравиться