Vous êtes sur la page 1sur 21

2: Introduction to Android

2.1: Favourite of Consumers and Developers


2.2: Tools for Creating Apps
2.2.1: IDE:
2.2.2: App Marketplace
2.3: Downloading the Development
2.3.1: Installing the JDK
2.3.2: Installing Eclipse IDE for Android Development
2.3.3: Installing the Android SDK
2.4 Creating an Android Project
2.5: Setting up an Emulator
2.6: Running the app in the Emulator
2.7: Introducing the Android Manifest
2.7.1: What is an Android Manifest
2.7.2: Structure of the Manifest File
2.7.3: Elements of the manifest

Copyright Trainedge All Rights Reserved


2: Introduction to Android
Android is an operating system based on Linux kernel. It is designed for mobile phones and
tablets. Android is the world's most widely used smartphone platform. Android's user
interface is based on touch inputs to manipulate on-screen objects. Android does not
differentiate between the phone’s core applications and third-party applications. They can all
be built to have equal access to a phone’s capabilities providing users with a broad spectrum
of applications and services. Android provides access to a wide range of useful libraries and
tools that can be used to develop applications with a wide variety of features.

2.1: Favourite of Consumers and Developers


Android is the most popular mobile operating system in the world with more than 1.5 billion
app downloads each month. Several software, hardware and telecom companies around the
world support the Android operating system and are continuously working on improving the
quality of devices and applications. The Android logo is shown below:

2.2: Tools for Creating Apps


Android has a rich set of development tools to build high quality apps for mobiles and
tablets. You can offer your app to millions of users using a single application model.

Android helps you take advantage of the hardware capabilities available on each device. It
automatically adapts your UI for each device while still giving you control over the UI.

With Android, you can create a single app that looks and works great on both phone and
tablet. You declare your UI in XML resources, one set for parts of the UI that are common
and another set for device specific optimization. At runtime, Android applies the correct
resource sets based on device parameters and specifications.

To help you develop efficiently, the Android Developer Tools offer a full Java IDE (Integrated
Development Environment) with advanced features for developing, debugging, and
packaging Android apps. You can use the IDE to develop your app for any available Android
device. You can also emulate any hardware device to test and optimize your app.

2.2.1: IDE:

An integrated development environment (IDE) is a software application that provides


comprehensive facilities to computer programmers for software development. An IDE
normally consists of a source code editor, build automation tools and a debugger. Some

Copyright Trainedge All Rights Reserved


examples of IDE are Eclipse, ActiveState Komodo, Oracle JDeveloper, NetBeans, Microsoft
Visual Studio, Xcode and Delphi.

2.2.2: App Marketplace

Google Play is the most important marketplace for selling and distributing Android apps.
Google Play is owned and managed by Google. All Android devices can connect to Google
Play and download apps from it.

Google Play provides lot of flexibility in publishing, pricing and distributing your app. You can
decide which devices you want to make your app available on, Which countries you want to
sell in and how much to charge for it. You can sell your app for a fixed price or offer it as a
subscription service. You can also include in-app purchase and advertising to monetize your
app.

2.3: Downloading the Development


You can develop Android apps on any computer that runs one of the following operating
systems:

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

Mac OS X (Intel) 10.5.8 (x86 only)

Linux (i386)

2.3.1: Installing the JDK

The Java Development Kit (JDK) is an implementation of either one of the Java SE, Java EE
or Java ME platforms released by Oracle Corporation in the form of a binary product aimed
at Java developers on Solaris, Linux, Mac OS X or Windows.

Follow these steps to install the JDK:

1. Go to http://www.oracle.com/technetwork/java/javase/downloads/index.html in
your browser.

The Java SE downloads page appears.

2. Click the JDK link under the Java Platform (JDK) heading

If you’re on a Mac, install the JDK through Software Update panel. A new Java SE
downloads page appears, asking you to specify which platform (Windows, Linux, or Mac)
you’ll be using for your development work.

Copyright Trainedge All Rights Reserved


3. Download the JDK for the platform or operating system you are using.
4. Click .exe to download the file, for example : JDK-6u20-windows-i586 for 32 bit
systems.

Windows opens a message box with a security warning as shown below.

5. In the Save As dialog box, select the location where you want to save the file
and click Save.

You should install the correct Java SDK version. Android 4.2.2 supports Java SDK versions
6 and 7.

When the download is complete, double-click the file to install the JDK. A dialog box appears
that asks whether you want to allow the program to make changes to your computer. Click
the Yes button.

Copyright Trainedge All Rights Reserved


2.3.2: Installing Eclipse IDE for Android Development

1. If you don’t already have Eclipse, you can download it from the Eclipse website -
http://www.eclipse.org/downloads/index.php

2. Unzip the file that says eclipse-java-kepler-R-win32-x86_64.zip (for 64 bit systems)

It will create a folder named eclipse.

You can create a shortcut on your desktop on your Windows machine to


the eclipse.exe file by Right-clicking on the eclipse.exe file. From the contextual
menu, select Send to and then Desktop (create shortcut).

1. Open Eclipse by double clicking it and a Workspace Launcher window will appear.
The window will display the default path for the workspace as C:\Documents and
Settings\username\workspace (where username is your login on the machine).
Change the location for the workspace if you want.

2. Check the box labelled Use this as the default and do not ask again. If you ever
want to re-enable the display of this window, once Eclipse starts, you can

o Select Window | Preferences.

o Double-click General or the + symbol.

o Click Startup and Shutdown

o Check the box labelled prompt for workspace on startup.

3. Click OK

4. Download Course Library Jar into the same folder that you are using as your
workspace or move this file in the workspace folder after downloading it elsewhere.

5. In Eclipse, select Windows | Preferences

6. Click the in front of Java or double-click Java

7. Click Installed JREs

8. Under the Name column, double-click jre1.6.

9. Click the Add External Jars...button.

10. In the Jar Selection window, navigate to your workspace folder, and double-click the
file cs15-1xx.library.

11. In the Installed JREs window, click OK.

12. Close the Welcome window. You can always get it back by selecting Help |
Welcome

13. Terminate the Eclipse window.

Copyright Trainedge All Rights Reserved


2.3.3: Installing the Android SDK

The Android SDK (Software Development Kit) includes a debugger, Android libraries, a
device emulator, documentation, sample code, and tutorials. The SDK ensures that your
code will have all the libraries needed to run your app on an Android device. This course
uses the official Android SDK. It is strongly recommended that you use the latest SDK for
developing your app.

Downloading the Android SDK

To download the Android SDK, follow these steps:

1. Go to http://developer.android.com/sdk/index.html in the browser.


2. Choose the latest version of the SDK starter package for your platform.
3. Extract the SDK.
4. Navigate to the directory where you extracted the SDK, and double-click SDK Setup

5. If you’re prompted to accept the authenticity of the file, click Yes. The Android SDK
and AVD Manager dialog box opens.
6. Select any SDK Platform from Android 2.2 to Android 4.3 check box.

2.2 is the basic version of Android and 4.3 is the latest and greatest version of Android. You
should also check the boxes for the documentation and samples that correspond with
Android version.

Copyright Trainedge All Rights Reserved


7. Click Install Selected.
8. Select the Accept radio button to accept the license and then click Install

9. In the next dialog box, select Accept and click Install.

The Installing Archives dialog box opens, displaying a progress bar

Copyright Trainedge All Rights Reserved


10. When the archives installation is complete, click the Close button.

While the Android SDK is attempting to connect to the servers to obtain the files, you may
occasionally receive a Failure to fetch URL error. If this happens, navigate to Settings, select
Force https:// Sources to be Fetched Using http://, and then attempt to download the
available packages again.

Installing the Eclipse Plugin

Android offers a custom plugin for the Eclipse IDE, called Android Development Tools
(ADT). This plugin provides a powerful, integrated environment in which you develop
Android apps. It extends the capabilities of Eclipse to let you quickly set up new Android
projects, build an app UI, debug your app, and export signed (or unsigned) app packages
(APKs) for distribution.

If you need to install Eclipse, you can download it from eclipse.org/downloads/.

If you prefer to work in a different IDE, you do not need to install Eclipse or ADT. Instead,
you can directly use the SDK tools to build and debug your application.

Download the ADT Plugin

1. Start Eclipse, then select Help > Install New Software.

2. Click Add, in the top-right corner.

3. In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the
following URL for the Location:

https://dl-ssl.google.com/android/eclipse/

4. Click OK.

If you have trouble acquiring the plugin, try using "http" in the Location URL, instead
of "https".

Copyright Trainedge All Rights Reserved


5. In the Available Software dialog, select the checkbox next to Developer Tools and
click Next.

6. In the next window, you'll see a list of the tools to be downloaded. Click Next.

7. Read and accept the license agreements, then click Finish.

If you get a security warning saying that the authenticity or validity of the software
can't be established, click OK.

8. When the installation completes, restart Eclipse.

Configure the ADT Plugin

Once Eclipse restarts, you must specify the location of your Android SDK directory:

1. In the "Welcome to Android Development" window that appears, select Use existing
SDKs.

2. Browse and select the location of the Android SDK directory you recently
downloaded and unpacked.

3. Click Next.

Your Eclipse IDE is now set up to develop Android apps, but you need to add the latest SDK
platform tools and an Android platform to your environment. To get these packages for your
SDK, continue to Adding Platforms and Packages.

Troubleshooting ADT Installation

If you are having trouble downloading the ADT plugin after following the steps above, here
are some suggestions:

 If Eclipse cannot find the remote update site containing the ADT plugin, try changing
the remote site URL to use http, rather than https. That is, set the Location for the
remote site to:

http://dl-ssl.google.com/android/eclipse/

 If you are behind a firewall, make sure that you have properly configured your proxy
settings in Eclipse. In Eclipse, you can configure proxy information from the main
Eclipse menu in Window > Preferences > General > Network Connections.

If you are still unable to use Eclipse to download the ADT plugin as a remote update site,
you can download the ADT zip file to your local machine and manually install it:

Package Size MD5 Checksum

ADT-22.3.0.zip 16839757 bytes 1097fccf32063e3638a9d27aa0f295ca

Copyright Trainedge All Rights Reserved


1. Download the ADT Plugin zip file (do not unpack it):

2. Start Eclipse, then select Help > Install New Software.

3. Click Add, in the top-right corner.

4. In the Add Repository dialog, click Archive.

5. Select the downloaded ADT-22.3.0.zip file and click OK.

6. Enter "ADT Plugin" for the name and click OK.

7. In the Available Software dialog, select the checkbox next to Developer Tools and
click Next.

8. In the next window, you'll see a list of the tools to be downloaded. Click Next.

9. Read and accept the license agreements, then click Finish.

If you get a security warning saying that the authenticity or validity of the software
can't be established, click OK.

10. When the installation completes, restart Eclipse.

To update your plugin once you've installed using the zip file, you will have to follow
these steps again instead of the default update instructions.

2.4 Creating an Android Project


An Android project contains all source code for your Android app.

You will now create a new project using Eclipse with the ADT plugin. The Android SDK is
installed with your Eclipse.

1. Click New in the toolbar.

2. In the New window, under Android folder, select Android Application Project, and
click Next.

Copyright Trainedge All Rights Reserved


3. In the New Android Application wizard, fill in the information for the News Reader
App:

o Application Name is the app name users see in the app store or on the
phone. Enter "News Reader".

o Project Name is the name of your project directory on your computer and in
Eclipse. Enter News Reader App.

o Package Name is the package namespace for your app. You must give a
unique package name for each app. Enter "com.yourname.newsreader."
Replace “yourname” with your name in this.

o Minimum Required SDK is the oldest version of Android that you want to
support for your app. You use API level to tell Android the oldest supported
version for your app. If you want to support more devices, keep this as low as
you can without impacting the app’s performance. Set it to 2.3.

o Target SDK is the Highest Android version you wish to support. You use
the API level for this also. You should try to use the latest version in order to
support new features of Android. Set it to 4.3.

o Compile With is the Android version against which you will compile your app.
By default, this is same as the SDK version installed on your computer. You
can compile your app to support older versions, but compiling for the latest
version will enable your app to use the new features and support new
devices. You do not need to set it as you will compile the news Reader app
for Android 4.3.

o Theme specifies the Android UI style to that your app will use.

Click Next.

Copyright Trainedge All Rights Reserved


4. On the next screen to configure the project, leave the default selections and
click Next.

5. You can create a custom launcher icon for your app. For now, you will accept the
default icon so unselect the check box Create custom launcher icon. You will
create a custom launcher icon in chapter 4. Click Next.

6. On the Create Activity dialog box, you can select an activity template to start
building your app. Select BlankActivity and click Next.

7. Accept default values for all fields and click Finish.

You are now ready to start building the News Reader app with default files and settings.

2.5: Setting up an Emulator


First, you need to create an Android Virtual Device (AVD), also known as an emulator. An
AVD is a virtual Android device that looks and acts just like a real Android device. AVDs can
be configured to run any particular version of Android as long as the SDK for that version is
downloaded and installed. Follow these steps to create your first AVD:

1. To open the AVD Manager, click the icon on the Eclipse toolbar.

The AVD Manager opens

2. Click the New button. The Create New Android Virtual Device (AVD) dialog box opens

Copyright Trainedge All Rights Reserved


3. For this AVD, in the Name field, type Default_HVGA.

4. In the Target box, select Android version.

5. In the SD Card section, leave the fields blank. You have no use for an SD Card in your
application.

6. Don’t select any new features in the Hardware section.

The Hardware section outlines the hardware features your AVD should emulate. You don’t
need any extra hardware configuration for this application.

7. Click OK. The Android AVD Manager dialog box should now look like this

8. Close the Android SDK and AVD Manager dialog box.

Copyright Trainedge All Rights Reserved


You may receive an error message after you create your AVD. This message may say
Android requires .class compatibility set to 5.0. Please fix project properties. If this happens
to you, you can fix it by right-clicking the project in Eclipse and choosing Android Tools > Fix
Project Properties from the context menu.

2.6: Running the app in the Emulator


You start your application by selecting Run > Run or pressing Ctrl+F11. Both will launch the
application in an emulator using the default launch configuration. The ADT compiles your
application and then deploys it to the emulator. If you didn’t create a launch configuration,
you see the Run As dialog box. Choose Android Application, and a launch configuration is
created for you.

You will see the emulator loading:

Copyright Trainedge All Rights Reserved


The emulator has many boot screens. The third and final screen you see is the loaded
emulator:

Once emulator has loaded, the locked home screen appears. To unlock the home screen,
click and drag the Lock icon to the right side of the screen. When the icon reaches the far
side of the screen, release the icon.

After the emulator is unlocked, the home screen appears:

The ADT now starts the News Reader application for you.

Copyright Trainedge All Rights Reserved


You can view the status of the installation via the Console view in Eclipse:

2.7: Introducing the Android Manifest

2.7.1: What is an Android Manifest

Every Android project includes a special file called the Android manifest file. The Android
system uses this file to determine application configuration settings, including the
application’s identity as well as what permissions the application requires to run. We will

Copyright Trainedge All Rights Reserved


examine the Android manifest file in detail and look at how different applications use its
features.

The manifest is made up of a root manifest tag with a package attribute set to the project’s
package. It usually includes an xmlns: android attribute that supplies several system
attributes used within the file. A typical manifest node is shown in the XML snippet below:

<manifest xmlns:android=http://schemas.android.com/apk/res/android

package=”com.yourname.newsreader”>

[... manifest nodes ... ]

</manifest>

2.7.2: Structure of the Manifest File

<?xml version="1.0" encoding="utf-8"?>

<manifest>

<uses-permission />
<permission />
<permission-tree />
<permission-group />
<instrumentation />
<uses-sdk />
<uses-configuration />
<uses-feature />
<supports-screens />
<compatible-screens />
<supports-gl-texture />

<application>

<activity>
<intent-filter>
<action />
<category />
<data />
</intent-filter>
<meta-data />
</activity>

<activity-alias>
<intent-filter> . . . </intent-filter>
<meta-data />
</activity-alias>

Copyright Trainedge All Rights Reserved


<service>
<intent-filter> . . . </intent-filter>
<meta-data/>
</service>

<receiver>
<intent-filter> . . . </intent-filter>
<meta-data />
</receiver>

<provider>
<grant-uri-permission />
<meta-data />
<path-permission />
</provider>

<uses-library />

</application>

</manifest>

2.7.3: Elements of the manifest

Application: A manifest can contain only one application node. It uses attributes to specify
the metadata for your application including its title, icon, and theme. It also acts as a
container for other components.

<application android:icon=”@drawable/icon” android:theme=”@style/news”>

[... application nodes ...]

</application>

Activity: An activity tag is needed for every Activity displayed by your application. The class
name can be specified using the android: name attribute. Trying to start an Activity that’s not
defined in the manifest will throw a runtime exception.

<activity android: name=”.MainActivity” android:label=”@string/NewsReader”>

<intent-filter>

<action android: name=”android.intent.action.MAIN” />

<category android: name=”android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

Copyright Trainedge All Rights Reserved


Service: There should be a service tag for each Service class used in your application.
Service tags support intent-filter child tags to allow late runtime binding.

<service android: enabled=”true” android: name=”.NewsService”></service>

Provider: Content Providers are used to manage database access and sharing within and
between applications.

<provider android: permission=”com.yourname.Your_PERMISSION”

android: name=”.NewsProvider”

android: enabled=”true”

android: authorities=”com.yourname.newsreader.NewsProvider”>

</provider>

Receiver: By adding a receiver tag, you can register a Broadcast Receiver without having to
launch your application first. By registering a Broadcast Receiver in the manifest, you can
make this process entirely autonomous. If a matching Intent is broadcast, your application
will be started automatically and the registered Broadcast Receiver will be run.

<receiver android: enabled=”true”

android: label=”Content Receiver”

android: name=”.ContentReceiver”>

</receiver>

Uses-permission: Uses-permission tags declare the permissions you’ve determined that


your application needs for it to operate properly. The permissions you include will be
presented to the user, to grant or deny, during installation.

<uses-permission android: name=”android.permission.ACCESS_LOCATION”>

</uses-permission>

Permission: Permission tags are used to restrict the access to an application component.
Application components can then add them by using the android: permission attribute.
Within the permission tag, you can specify the level of access the permission will permit, a
label, and an external resource containing the description that explain the risks of granting
this permission.

<permission android:name=”com.yourname.DETONATE_DEVICE”

android: protectionLevel=”dangerous”

android: label=”Self Destruct”

android: description=”@string/detonate_description”>

</permission>

Copyright Trainedge All Rights Reserved


Instrumentation: Instrumentation classes provide a framework for running tests on your
Activities and Services at run time. They provide hooks to monitor your application and its
interaction with the system resources.

<instrumentation android: label=” Testing”

android: name=”.TestClass”

android: targetPackage=”com.yourname.newsreader”>

</instrumentation>

Defining manifest for your app

To use the Manifest Editor in Eclipse, right-click the AndroidManifest.xml file in your project,
and select Open With ➪ Android Manifest Editor. This presents the Android Manifest
Overview screen, as shown below:

This gives you a high-level view of your application structure and provides shortcut links to
the Application, Permissions, Instrumentation, and raw XML screens.

Application tab, shown below is of particular interest:

Copyright Trainedge All Rights Reserved


You use it to manage the application node and the application component hierarchy, where
you specify the application components. You can specify an application’s attributes like Icon,
Label, and Theme in the Application Attributes panel. The Application Nodes tree beneath it
lets you manage the application components, including their attributes and any associated
Intent Filter sub-nodes.

Copyright Trainedge All Rights Reserved

Vous aimerez peut-être aussi