Saturday, June 15, 2013

Android Application and its components

In the last post we created an android application. Let us see some features of an android application.

  • Android OS is a multi-user Linux OS.
  • Each  application has its own user id.
  • Each process in the application runs in its own virtual machine. But android apps do not use JVM. They use a VM called dalvik virtual machine.
  • Every application runs in its own process
  • Android implements principle of least privilege. That is to say each application can access its own data and resources, but not the those of other applications.
  • But apps can access device data such as contacts, SMS etc by requesting permission from the user at install time.
Components of an application

An application can consist of
  • Activity : single screen with user interface. An app can start any of its activities and also activities of other apps. 
  • Service : It is a background component which does a long running operation like conversion from text to speech. It does not have a user interface.
  • Content provider : this manages shared data. Your app can store the data in file, sqlite database etc and other apps can access this data using content provider.
  • Broadcast receiver : receives system wide broadcast announcement like turning off of the display, arrival of a SMS message etc. Does not have user interface, but can display a message on status bar.
Starting application components :

     Activity, Service or Broadcast receivers are started by sending an asynchronous message called intent. Content provider is started when it is targeted by a content resolver. In some cases, if the intent object is provided with the action to perform, the android system selects appropriate activity and starts it. e.g. if the intent specifies view action for a web page, then the browser is started.


Manifest file :

 All components of an activity must be mentioned in a file called AndroidManifest.xml file. This manifest file must be located at the root of the android application. If a component is not mentioned in this manifest file, then android system can not start that component.

A sample manifest file looks like this
 As you can see, the manifest file also specifies the minimum sdk version, the permissions for the app, the hardware requirements like bluetooth, camera etc.

Resources

If you look at the app in package explorer, you can see along with src (source) folder, another folder res (resource). The resource folder contains the images for the app, layout files for activities, menus, styles, colors etc. Resources are stored in different sub-folders such as res/layout/main_activity.xml, res/drawable/ldpi/logo.png etc. Sdk tools will create a unique resource id for each of the resources in the form - R.drawable.logo, R.layout.main_activity etc.





















And to end our discussion, let us see what are the various components android sdk provides us for development.

SDK Tools

Emulator:
You can test run your apps on emulator, instead of actual phones. You can select a virtual device with different versions of android, different hardware configuration. The emulator can be started from eclipse or from shell. The emulator can even emulate sms messages, internet etc, but not camera or bluetooth.

DDMS : Dalvik debug monitoring service lets you debug your application. You can inspect the memory, you can kill any process, you can set break points etc.

ADB : Android debug bridge lets you communicate with the android emulator or device. Adb provides you commands to install and uninstall an app, push or pull a file, start a linux shell for the device or virtual device.

AAPT : Android asset packaging tool converts all the resource file of android application into aidl file. Java compiler converts aidl file and java source file together into .class files.

dex :  dex tool converts .class file into dalvik byte code called .dex file.

apk builder : converts .dex file into file .apk file which can be installed on the phones.

Don't worry too much about these terminology. Eclipse will take care of all these tools. And if you want to debug the application, you can use ddms perspective.


Saturday, June 1, 2013

Create your first android application

I am not going to bore you with details like what is android and how it is better than other platforms etc. Since you have already decided to learn android, you have done your homework and got some introduction about these topics.

Let me instead start with what are the hardware and software you need to learn android.

Hardware
  1.  You need a PC. The OS does not matter. You can use windows machine, or Linux machine or mac.
 That's all. At this stage you do not need a phone running android.

Software 
  1. Since android applications are written in Java, you need a Java Development Kit. You can download this from oracle site http://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. You need android software development kit (SDK). This can be downloaded from android developer website http://developer.android.com/sdk/index.html
  3. It is preferable to use an IDE. Eclipse is very convenient. You can download latest version of eclipse from http://www.eclipse.org/downloads/
Download these software and install them. And you are almost ready to learn android.

Do you have some basic knowledge of Java programming? If not, learn  Java now. Since android programs are written in Java, you need to know Java.

You will again ask me- what about emulator, where do I get emulator from. Android SDK comes with emulator and DDMS -Dalvik debug monitor server. 

Earlier it was necessary to download ADT - android developer tools, in eclipse IDE. But now android sdk comes bundled with ADT.

Once you install android sdk, you should decide which version of android - 4.2 Jelly Bean being the latest, you want to work on. Run SDK manager and download the corresponding sdk platform

You should download platform-tools and SDK platform form whichever version of android you want to use. It is advisable to download documentation which will help you in syntaxes.  Here I have selected android 4.0.3 to download and samples for the same.

Create  your first android application

You have armed yourself with all the tools necessary. Now plunge into water. Start eclipse and start a new android project

In Eclipse menu select File-New-Android Application Project.

In latest eclipse version, this is how the menu looks like


Change the application name and package name. In the next screen, select create Activity and create application in workspace. In third screen, select create blank activity and in fourth screen give activity name and xml name and click finish.
Your first android app is ready. Now build the project. Right click on the project name in project explorer and select run as android application. 
A new emulator is started and your app will be installed and executed in it. 



This is how your first app looks in android 4.2 emulator. Now grab a cuppa and enjoy. Because second lesson will not be this easy.