Saturday, February 23, 2013

Learning Android





INTRODUCTION


 Android is an  Linux-Based  Open Source  Operating System  with a Java programming interface. Initially developed by Android, Inc.,and later purchased by Google in 2005.The Open Handset Alliance  released the Google Android SDK on November 12th, 2007, having announced it about a week before. The impact was unbelievable.


Open Handset Alliance is an association of  companies to develop open standards for mobile devices.Member  include Google,HTC, Sony, Dell, Intel, Motorola, Qualcomm, Texas Instruments,Samsung Electronics, LG Electronics ,etc.


  Versions

1.6                Donut                                   September 15, 2009

2.0                Eclair                                    October 26, 2009

2.2                Froyo                                    May 20, 2010

2.3               Gingerbread                         December 6, 2010

3.1               Honeycomb                           May 10, 2011

4.0.x            Ice Cream Sandwich            December 16, 2011

4.1.x             Jelly Bean                               July 9, 2012


Android Complete SetUP Link

Basics of Android

Android  SDK:The Android SDK provides you the API libraries and developer tools necessary to build, test, and debug apps for Android.SDK include a debugger, libraries, a handset emulator, documentation, sample code, and tutorials.


The whole SDK is supported on the following operating systems:

  • Microsoft platform (XP or Vista)
  • Mac OS X 10.4.8 or later (x86 machines only)
  • Linux

Android  NDK:The NDK is a toolset that allows you to implement parts of your app using native-code languages such as C and C++. For certain types of apps, this can be helpful so you can reuse existing code libraries written in these languages, but most apps do not need the Android NDK.

Android Architecture


The Android OS is a software stack of different layers, where each layer is a group of several  program components. Together it includes operating system, middle ware and important applications.



Layers in the Android stack: 

  • Linux Kernel Layer 
  • Libraries Layer 
  • Application Framework Layer 
  • Applications layer .
Linux Kernel:  Android OS is built on top of the Linux Kernel. 
Android takes Linux 2.6 as its underlying kernel and makes enhancements to meet mobile O/S requirements such as better memory management, better processor management.

What is the Kernel?
A kernel is the lowest level of easily replaceable software that interfaces 
with the hardware in your computer. It is responsible for interfacing all of 
your applications down to the physical hardware.



Core functionality
   •Memory management
   •Process management
   •Networking
   •Security settings
     Hardware drivers

Libraries: These libraries are written in c or c++ language and are specific for a particular hardware and library layer is interfaced through Java.

example:
SQLite: SQLite is the database engine used in android for data storage purposes
WebKit: It is the browser engine used to display HTML content

OpenGL: Used to render 2D or 3D graphics content to the screen


Dalvik is open-source software. It is the  responsible for running apps on Android devices. 
A type of Java Virtual Machine,Register based (not stack machine like JVM)
It is optimized for low memory requirements. 
It has been designed to allow multiple VM instances to run at once.
It Executes .dex (Dalvik-Executable) files instead of .class

DX tool converts classes to .dex format

Application Framework :Our applications directly interact with these blocks of the Android architecture. These programs manage the basic functions of phone like resource management, voice call management etc.

Blocks of Application Framework:

Activity Manager: Manages the activity life cycle of applications. To understand the Activity component in Android in detail click here
Content Providers: Manage the data sharing between applications. Our Post on Content Provider component describes this in greater detail
Telephony Manager: Manages all voice calls. We use telephony manager if we want to access voice calls in our application.
Location Manager: Location management, using GPS or cell tower
Resource Manager: Manage the various types of resources we use in our Application.

Applications layer: This is where our applications are placed.

Android application package file (APK) is a file generated during the build process. 
 Android projects are compiled and packaged into an .apk file, It contains all of the information necessary to run your application on a device or emulator

An APK file is an archive that usually contains the following folders:
  •  META-INF  Directory:
  •  MANIFEST.MF:  The Manifest file
  •  CERT.RSA: The certificate of the application.
  •   res: The directory containing resources not compiled into resources.arsc.
  •  AndroidManifest.xml:  An additional Android manifest file, describing the name,   version, access rights, referenced library files for the application.
  • classes.dex: The classes compiled in the dex file format understandable by the Dalvik virtual machine
  • resources.arsc : a file containing pre-compiled resources, such as binary XML for example.

AndroidManifest.xml: This file gives essential information about the application to the Android system, It contains information about your application package,application version, app name,app icon etc.



Dalvik virtual machine: Is register based virtual machine,on Android operating system, 
multiple Dalvik VM instances are running at the same time. Each service or application runs into its own virtual machine and each one on a different process.

The  differences between Dalvik and a standard Java virtual machine (JVM) is that Dalvik is register-based, whereas JVMs are stack-based. Register-based virtual machines allows greater time optimization










There are four building blocks to an Android application:

  •  Activity
  •  Broadcast Receiver
  •  Service
  •  Content Provider




Not every application needs to have all four, but your application will be written with some combination of these.Once you have decided what components you need for your application, you should list them AndroidManifest.xml.

Activity :  An activity is the visual representation of an Android application, 
provides a screen with which users can interact in order to do something.


An Android activity is both a unit of user interaction—typically filling the whole screen of an Android mobile device and a unit of execution.To create an activity, you must create a subclass of Activity (or an existing subclass of it).All activity classes must have a corresponding <activity> declaration in their package's AndroidManifest.xml.

Activity Life Cycle :
The above Android Activity Lifecycle flow diagram can be explained as follows:


When we launch an Activity in Android, it first calls the onCreate() method. This is where we do User Interface creation and initialization of data elements. This method is provided with Bundle object as parameter to restore the UI state.

onStart() method is called before the Activity is being visible to the User. Remember that Activity is still not Active.

With the onResume() method, the Activity become visible and Active for the user to interact with. The Activity will be at the top of the Activity stack at this point. Now the Activity is in running /active state and is able to receive user inputs.

In the Active state, onPause() method will be called when the system is about to resume another Activity on top of this one or when the user is about to navigate to some other other parts of the system. It is the last guaranteed call to a method before the Activity  can get killed by the system. That is, there’s a possibility that your activity may be killed by the system at the paused state without executing any further method calls. 

By default, an Activity can remain in the paused state if:
  • The user has pressed the home button
  • Another activity or notification which is on top of it doesn’t completely obscures the visibility of underlying Activity.
  • The device goes to sleep.
onStop() called when the activity is no longer visible to the user in either the foreground or background.

onRestart()  called when the activity is being restarted (made visible again) after executing onStop but not onDestroy.

onDestroy() called just before an activity is destroyed/unloaded from memory.

Any activity that isn't currently "running" could be removed from memory by the Android OS if system memory is low. even if  an activity that is "paused" can be destroyed under extremely low memory conditions, 
Best practice is to run code to store/save any persistent data .you can use code in the onSaveInstanceState method to perserve that data in a Bundle object 
the onSaveInstanceState method in the activity is called by the Android OS itself as soon as the activity has the potential to be removed from memory.

However, onSaveInstanceState is NOT called when the user explicitly takes action to destroy an activity.

The transistion from one activity to another is accomplished through the use of asynchronous messages called Intents. An intent can be used to pass data from one activity to another, and that data is structured using key/value pairs.


Android Service:

A service is a android component which runs in the background, service has no user interface and  user interaction,it is not bound to the lifecycle of an activity.

A Service perform a longer-running operation with not interacting with the user while the user is in a different application.

Each service class must have a corresponding <service> declaration in its package's AndroidManifest.xml. 

Services can be started with Context.startService() and Context.bindService().  Services run with a higher priority than inactive or invisible activities and therefore it is less likely that the Android system terminates them.

Android Storage:-  Android provides several options to store data. you have to choose depending on your specific needs, such as whether the data should not be accessed by other application and how much space your data requires.


  • Shared Preferences: Store private primitive data in key-value pairs.
  • Internal Storage:Store private data on the device memory.
  • External Storage: Store public data on the shared external storage.
  • SQLite Databases: Store structured data in a private database.

Layouts:-

A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. You can declare a layout in two ways:

  • Declare UI elements in XML:- Android provides a straightforward XML vocabulary that corresponds to the View classes and sub-classes, such as those for widgets and layouts.  
  • Instantiate layout elements at run time:-                                                                                  Your application can create View and ViewGroup objects (and manipulate their    properties) programmatically.


The Android framework gives you the flexibility to use either or both of these methods for declaring and managing your application's UI. The most convenient and maintainable way to design application user interfaces is by creating XML layout resources. This method greatly simplifies the UI design process.

The standard Layouts are:

  • Absolute Layout:-   based on the simple idea of placing each control at an absolute position.  You specify the exact x and y coordinates on the screen for each control.  This is not recommended for most UI development (in fact Absolute Layout is currently deprecated)


  • Frame Layout:-designed to display a single item at a time. You can have multiple elements within a Frame Layout but each element will be positioned based on the top left of the screen. Elements that overlap will be displayed overlapping.


  • LinearLayout:-A LinearLayout is a very simple layout that just arranges all its children in either a vertical column, or a horizontal row, depending on how it is specified. 


  • RelativeLayout:-RelativeLayout lays out elements based on their relationships with one another and with the parent container.


  • TableLayout:-TableLayout organizes content into rows and columns. The rows are defined in the layout XML, and the columns are determined automatically by Android.






Android Video Tutorial Link:

http://marakana.com/s/post/1017/android_bootcamp_series_2012_video_tutorial

http://www.learnerstv.com/Free-Computer-Science-Video-lectures-ltv461-Page1.htm

http://thenewboston.org/list.php?cat=6