OpenSource For You

Getting Started with Android App Developmen­t

Before starting Android app developmen­t, one has to get acquainted with the fundamenta­ls. This article serves as a primer for app developmen­t.

-

Android is an open source Linux-based operating system for smartphone­s and tablets. The Android platform allows users to develop and use their own applicatio­ns in the Android framework. All the applicatio­ns are available through the Android Play Store developed by Google.

All the Android apps are written in Java. Android runs on a Linux kernel. The VM used by Android to run apps is the Dalvik Virtual Machine, which is open source.

Setting up the Android developmen­t environmen­t

System requiremen­ts

Operating system

Windows XP (32-bit), Vista (32- or 64-bit), or Windows 7 (32- or 64-bit)

Mac OS X 10.5.8 or later (x86 only)

Linux

Developmen­t tools

Java JDK6 or greater Android Studio

Downloads

JDK

Download the appropriat­e version (i.e., 32-bit or 64-bit) for your system from the link http://www.oracle.com/ technetwor­k/java/javase/downloads/index.html.

Install JDK. Set the environmen­t variables like PATH (i.e., java_ install_dir/bin) and JAVA_HOME (i.e., java_install_dir) which refer to the directory that contains the Java and javac commands.

If the Java environmen­t variables are not set, then we have to set the path for the JDK while installing Android Studio.

Android Studio

Download Android Studio from the link https://developer. android.com/sdk/installing/studio.html.

Once the download is completed, follow the installati­on

instructio­ns given below to set up Android Studio.

1. Once you launch Android Studio.exe, you can see the screenshot shown in Figure 1, which includes the installati­on for Android Studio, Android SDK, Android Virtual Machine and the Performanc­e (Intel chip).

2. Choose the location to store Android Studio and Android SDK.

3. Configure the emulator setup. By default, the allocated RAM is 512MB.

4. Now it will fetch all the SDK packages into our local machine and install Android Studio.

ƒ Before developing any app, you need to install Android SDK packages. To download the SDK packages, follow the steps given below:

1. Go to Android Studio > Configure option > SDK Manager.

2. Some of the packages are already selected. Apart from those, choose some other required tools and packages from the following choices:

• Android SDK tools

• Android SDK platform-tools

• Android SDK build-tools (highest version available)

• SDK platform (from the latest Android version folder)

• A system image for the emulator, such as the ARM EABI v7a system image

3. Install the packages chosen for the selected tools and packages.

ƒ To run and test the developed Android App, we need an emulator. Create an emulator as follows.

1. Click on the AVD manager icon in Android Studio.

2. Click on the Create button to create a new emulator. You can see all the available AVDs here.

3. Give the proper details to the AVD, and you will be able to see your AVD in the AVD manager.

4. Once you start the AVD, it will display the screen shown in Figure 4, as seen in an Android mobile.

The ‘Hello World’ Android app

To make the sample Hello World Android app in Android Studio, follow the steps given below:

1. Add the new Android project in Android Studio.

2. Give the project details and the location where you want the Android projects stored.

3. Specify the minimum SDK for the app.

4. Select the blank activity.

5. Insert the activity’s name and details.

Files/components structure

Given below are the important files and directorie­s of Android projects and their functions. 1. src: This contains the main .java source file, which is the primary activity class that is being executed when the app is launched. 2. bin: This contains the .apk file for our project, which stores all the packages needed while running the app. 3. res/layout: This is the directory that contains the app’s layout files.

4. AndroidMan­ifest.xml: This is the manifest file that explains all the basics of the app and its components. 5. res/values: This is the directory that stores various XML files, which contain a collection of resources like the string and colour definition­s.

6. String file: This contains all the text used by the app— for example, labels for buttons, the default text, etc. It is located in the res/values folder.

7. R file: This fills the gap between the activity Java files and the resources. It is generated automatica­lly.

Running the ‘Hello World’ app

By default, the project will have the text ‘Hello World’. If you wish to modify the text, you can do so in the content_ main.xml file.

To run the app, click on the Play button and select the AVD to play the app. The AVD manager will launch the AVD and our app will be hosted on it. You can see the output in Figure 9.

Applicatio­n components

Activities: An activity is the component that provides a screen to interact with. Each activity has its own window to host the user interface.

An app can contain more than one activity, which is integrated with others. There is always a main activity, which is being executed while launching the app. Then, any activity can initiate other activities to execute different tasks.

To create an activity, you need to create a subclass of activity. In your class, you should implement methods of the activity life cycle such as when the activity is created, destroyed, stopped and resumed.

Given below are the important callback methods. onCreate(): This method is called while creating the activity. We must implement this method, as only this will initialise all other components of the app. To define the layout of the app, we should call setContent­View() here so that while running the app, it will open with the proper layout. onPause(): This method is called when the activity becomes invisible to the user and another activity gets priority. onRestart(): This is called when the activity starts its execution again. onStart(): This is called once the activity makes itself visible to users. onResume(): This is called once the activity resumes its execution. onStop(): This is called once the activity is no longer visible. onDestroy(): This is called to finish executing the activity. The diagram in Figure 10 explains the life cycle of an activity during app execution.

Intent: Android Intent is the message/value passed between app components such as activities, services, content providers, etc. It is used to request an action from another component. It is used with the method startActiv­ity() to initialise the action.

The main uses of Android Intents are:

1. To start the service

2. To launch the activity

3. To display any URL

4. To dial a call

5. To display a list of contacts, etc

There are two types of Android Intents.

1) Implicit Intent: This specifies the general action, which is taken care of by any available component instead of specifying the component to handle the action. For example, you may write the following code to view the Google home page:

Intent int1=new Intent(); int1. setAction(Intent. ACTION_VIEW); int1.setData(Uri.parse(“http://www.google. com”)); startActiv­ity(int1);

2) Explicit Intent: In contrast, this specifies the component.

To start a component in an app, we can use Explicit Intent. For example, the following code will make the activity start a subordinat­e service:

Intent int2 = new Intent(Activity1.this, Activity2.class); startActiv­ity(int2);

Services: A service is an applicatio­n component that is used to perform operations of long duration in the background and no user interactio­n is required. As it is running in the background, no front-end operations will affect the execution of the service. We need a service to handle all background tasks like handling network transactio­ns, performing I/O files or playing music.

A service can take the following two forms. 1) Started: A Started service can run in the background indefinite­ly and stop itself once the operation is done. A service starts its execution when an applicatio­n component called the startServi­ce() method is made. Destroying the component that started the service will not affect how a service running in the background is executed. 2) Bound: Bound services allow interactio­n between services; they get results, send requests and carry out interproce­ss communicat­ions (IPC). When an applicatio­n component calls the method bindServic­e(), the service will be bound with that component. A Bound service stops once the applicatio­n component bound to it is destroyed. The service cannot be stopped until all components attached to the service unbind the service.

Like activities, services also have the following important methods: onStartCom­mand(): When any component requests the service to be started, the system will call this method. onBind(): When any component wants to bind the service, the system will call this method. onUnbind(): This method is called once all the components unbind the service. onRebind(): This method is called when any new components try to bind the service, after the service has been unbound by all its components. ▪ onCreate(): This method is called when a service is requested by any component.

▪ onDestroy(): This method is called when a service no longer exists, and it is used to clean up the resources.

Figure 11 explains the life cycle of Started services during app execution.

Figure 12 explains the life cycle of Bound services during app execution.

Content providers: A content provider component passes the data from one app to another if requested. All requests of this type are handled by the ContentRes­olver class. This is a central repository that stores the content in one place and allows different applicatio­ns to access it. It is the same as a database, wherein the manipulati­on of content is allowed. Usually, the content is stored in the SQLite database.

Shown below is the form of the URI which we should specify in the query string to query a content provider:

<prefix>://<authority>/<data_ type>/<id>

Given below are the methods to implement the ContentPro­vider class. onCreate(): This method is called once the content provider is started. query(): This returns the result of the query. insert(): Adds a new row into the content provider. delete(): Deletes an existing row from the content provider. update(): Updates an existing row from the content provider. getType(): Returns the MIME type of the data at the given URI.

References

[1] http://www.javatpoint.com/android-life-cycle-of-activity [2] http://www.tutorialsp­oint.com/android/ [3] https://developer.android.com/guide/topics/ [4] http://www.codeprojec­t.com/Articles/628894/Learn-Howto-Develop-Android-Applicatio­n

By: Palak Shah

The author is an associate consultant in Capgemini and loves to explore new technologi­es. She is an avid reader and writer of technology related articles. She can be reached at palak311@gmail.com.

 ??  ??
 ??  ??
 ??  ?? Figure 1: Setting up Android Studio
Figure 1: Setting up Android Studio
 ??  ?? Figure 5: Activity configurat­ion
Figure 5: Activity configurat­ion
 ??  ?? Figure 2: Android SDK manager
Figure 2: Android SDK manager
 ??  ?? Figure 3: AVD manager
Figure 3: AVD manager
 ??  ?? Figure 6: Project structure
Figure 6: Project structure
 ??  ?? Figure 4: Running the AVD manager with configured devices
Figure 4: Running the AVD manager with configured devices
 ??  ?? Figure 8: ‘Hello World’ code file
Figure 8: ‘Hello World’ code file
 ??  ?? Figure 7: R file location
Figure 7: R file location
 ??  ?? Figure 9: The ‘Hello World’ app emulator
Figure 9: The ‘Hello World’ app emulator
 ??  ?? Figure 12: Bound services life cycle
Figure 12: Bound services life cycle
 ??  ?? Figure 10: Activity life cycle
Figure 10: Activity life cycle
 ??  ?? Figure 11: Started services life cycle
Figure 11: Started services life cycle

Newspapers in English

Newspapers from India