Getting Started - Android: Programming and App Development for Beginners (2015)

Android: Programming and App Development for Beginners (2015)

Chapter 2. Getting Started

Getting started with Android is quite simple and you can use any of the following systems to begin developing apps:

· Microsoft Windows XP or higher

· Mac OS X 1.5.8 or higher

· Linux, with GNU C Library 2.7 or higher

Everything you need to begin developing is available freely on the internet. You will need to download the following:

· Java Development Kit (JDK) 5 or higher

· Android Software Development Kit

· Java Runtime Environment (JRE) 6 or higher

· Android Studio

· Eclipse IDE For Java Developers

The last item on this list is optional but if you are using a Windows computer to develop your app, it will make things a little easier for you.

How to Set Up the Java Development Kit

Download the JDK from the above line and then follow the instructions given on the website for installing it – they are easy to follow. The next step is to set two environment variables, HOME and PATH, which refer to the directory containing Java and JavaC. These are usually found in java_instal_dir/bin and Java_install_dir respectively. If you are using a Windows PC, there are two ways to do this. The first is check where JDK is installed. Let’s say that you installed in C:\jdk1.6.0_15 (your reference numbers will be different depending on the version you installed), you would need to open up C:\autoexec.bat and add in this line (again, make sure you use the right version numbers):

· set PATH=C:\jdk1.7.0_75\bin;%PATH%

· set JAVA_HOME=C:\jdk1.7.0_75

The second way is to right-click on My Computer. From the menu, choose Properties and then click on Advanced System Settings. Click on the System tab and them on Environment Variables. Update the value for PATH and click on OK.

If you are using a Linux system and the SDK was installed in /usr/jdk1.6.0_15 and you are using C shell, you would need to open your .cshrc file and input this line:

· setenv PATH /usr/local/jdk1.7.0_75/bin:$PATH

· setenv JAVA_HOME /usr/local/jdk1.7.0_75

If you are using an Integrated Development Environment (IDE) Eclipse, it will already know where Java has been installed.

How Android is Configured

The Android operating system is a stack system made up of different components on four layers:

Linux Kernel

Right at the bottom of the stack is Linux, containing more than 100 patches. This is where the device hardware drivers are stores, such as the drivers needed for the keypad, camera and the display for example. The Linux kernel is also tasked with handling everything that Linux excels at, like networking and all the device drivers. This takes the sting out of having to interface with peripheral hardware

Libraries

Above the Linux kernel are the libraries. Included in these are the WebKit engine for the web browser, lib, which is a well-known library, the SQLite database for storing and share data, libraries need to record and play audio and video files, and the libraries that are responsible for security on the internet, to name just a few of the many that are there.

Android Libraries

Next up are the Android libraries, which hold the Java libraries needed for Android development. In here, you would find things like the application framework libraries a well as those that help the user with building an interface, drawing graphics and accessing databases. Some of the key libraries available for the developer are:

· android.app – gives the developer access to the app model and is the firm foundation of all applications developed for Android

· android.content – Makes the processes of accessing content, publishing and sending messages between apps and their components much easier

· android.database – Accesses data that is published by content providers. This is where you will find the SQLite management classes.

· android.opengl – Java interface for the OpenGL ES 3D graphics API

· android.os – Allows access to Provide applications along with access to operating system services like Messages, inter-process communication and system services

· android.text – allows text to be rendered and then manipulated on the display of the device

· android.view – The building blocks needed for user interfaces in an application

· android.widget – A collection of components for the user interface, already built, such as buttons, list views, radio buttons, layout managers an much more

· android.webkit – A collection of classes that are used to allow you to build the capability for web browsing into your application

Android Runtime

The next section contains one of the key components, called Dalvik Virtual Machine. This is a version of the Java Virtual Machine that has been designed specifically for Android. The Dalvik VM uses many of the core Linux features, like multi-threading and memory management, both of which are built in to the Java language. It VM allows all Android apps to run their own processes with their own version of the Dalvik virtual machine.

Android Runtime also has a number of core libraries, which are used to let app developers write their applications using Java language.

Application Framework

This layer of components includes a lot of high-level services, all of which are in the format of Java classes and all of which a developer can use in their app. The following key services are included:

· Activity Manager – Controls every aspect of the life cycle of the app and the activity stack

· Content Providers – The part that allows apps to both publish and share data with another application

· Resource Manager – Allows access to embedded resources that are not coded, like color settings, strings and interface layouts.

· Notifications Manager – Lets an app display notifications to the app user

· View System – A set of views that are used in the creation of the user interfaces in an app

Applications

The top layer of Android is where all the applications are and this is where your application will be installed – it can only be written to be installed in this layer.

The components of an application are the building blocks aht go to make it up. These are coupled very loosely by AndroidManifest.xml, which is an application manifest file that describe every part or block in the application and its interaction.

There are four main components that you can use when building your application:

· Activities – these say what the user interface will be and handle how the user will interact with it on their display

· Services – These are there to handle any of the background processes that are associated with your application

· Broadcast Receivers - These will handle all the communication between the applications and the operating system

· Content Provider - These will handle any management issues that arise with data and database management

Activities

An activity is used to present a screen that has a user interface on it, for actions to be performed by the user on their display screen. An example of this would be an email application. It could have an activity that lists all new emails while another activity would be used to compose an email and yet another for reading an email. If your application has more than a single activity, one of those activities must be earmarked as the one that is shown when the application is opened.

Activities are implemented as subclasses of the Activity class, as this example shows:

· public class MainActivity extends Activity {

· }

Services

Services are components that perform long-running operations in the background. An example of this would be a service that plays music while the user is on a different application. Or it could be fetching data via the network without stopping the user from doing something else.

Services are implemented as subclasses of the Service class, as this example shows:

· public class MyService extends Service {

· }

Broadcast Receivers

These are components that respond to messages broadcast from other applications or from the system itself. An example of this would an application that broadcasts to let another application know that new data has been downloaded and is ready for them to make use of. The receiver that intercepts the broadcast can take the appropriate action needed to make use of the data.

Broadcast receivers are implemented as subclasses of the BroadcastReceiver class and each separate message will be broadcasted as an Intent object:

· public class MyReceiver extends BroadcastReceiver {

· public void onReceive(context,intent){}

· }

Content Providers

This component takes data from one application and gives it to other applications when requested to do so. These requests are handled by methods that are contained in the ContentReceiver class. Each must implement a set of standard APIs that allow applications to carry out transactions.

· public class MyContentProvider extends ContentProvider {

· public void onCreate(){}

· }

You will begin to understand this more when you start to build your own app.

Additional Components

There are other components that can be used in building those components, in their logic and in the way that they are attached together so that they work properly. Those additional components are:

· Fragments – Representative of a part of the user interface in an Activity

· Views – Elements of the user interface that are shown on the screen, such as list forms and buttons

· Intents – Messages that connect two or more components together

· Resources – Elements that are external, such as constants, strings and drawable pictures

· Manifest - A configuration file for your application

Before we move on to the next chapter, where we start to actually write your first program, do make sure that your environment is set up properly. Once that’s been done, we can move on to write a simple Android application, one that you have all heard of, called “Hello, World!”