Vous êtes sur la page 1sur 17

Android

Android is an opensource operating system is Linux based and is suitable for touch screen mobile
devices. Android Inc. was a small startup company founded in Lapo Alto, California in October 2003 by
ANDY RUBIN, RICH MINER, NICK SEARS and CHRIS WHITE. Then Google acquired Android Inc.
in July 2005. Android uses Linux kernels under the hood that are wrapped by a Dalvik Virtual Machine
to applications written in JAVA.

1) Installation links
Download Java Installation Tutorial
Java Installation Link
Download Android Studio Installation Tutorial
Android Studio Installation Link

2) Folder Structure
While developing in Android, we will primarily code in two languages, XML and Java.
XML: This is used to create static content. This includes views, variable values and styles.
Java: The app logic is written in Java files. These can be in the form of Activities, Services, etc. We will
see these in more detail in the coming tutorials.
The XML files have a .xml extension and Java files have .java extension. The output file that we host on
Google Play/ transfer to Android phone has a .apk extension.

Let's now understand the folder structure of any Android app:


2.1) app: It contains the actual application code(source files, resource file and manifest files). Following
are the important sub-directories of the app folder:
2.1.1) src: Here you will see two sub-directories androidTest and main.
2.1.1.1) androidTest: The application code for testing purpose is stored in the
androidTest folder and is created by android automatically. This helps in building
testing packages without modifying the building files and the application code.
2.1.1.2) Main:
java: . We will see this directory in more detail in the next tutorial.
res: . This directory is where you will find the resources like drawable files, menu,
values ( style files, string values, dimension values). We will see this directory in
more detail in the coming sections.
2.1.2) libs: This is where external libraries are included.
2.2) Gradle scripts: Gradle is basically a compiler that merges all the resources and code and generates
the required executible (.apk) file.
Some of the important funcitons of the Gradle are as follows:
Specifying external libraries that are included in the app.
Version code and version number of the app.
Minimum Android version supported by the app

External Libraries: This is the place where all the referenced libraries and the information about the
targeted platform SDK are stored.
3) java Folder
This directory and all its sub directories hold the .java files. These files dictate the logic of the app.
This is how the java folder looks for a typical app.

4) res Folder
Resources are the additional files and static content that your code uses, such as bitmaps, layout
definitions, user interface strings, animation instructions, and more. The res folder contains these
resources in the following directories-
Sub-directories of res folder
anim/: For XML files that are compiled into animation objects.
color/: For XML files that describe colors.
drawable/:For image files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that
describe Drawable shapes or Drawable objects that contain multiple states (normal, pressed, or
focused).

mipmap/: For app launcher icons. This folder contains additional sub-folders for different
screen resolutions. Once an image is imported into this folder, Studio automatically resizes the
image into different resolutions and places them in the appropirate folders. This behavior allows
launcher apps to pick the best resolution icon for your app to display on the home screen. To see
how to import an image into the mipmap folder, please read about using 'Image asset'.

layout/: XML files that are compiled into screen layouts (or parts of a parent layout).
menu/: For XML files that define application menus.

values/: For XML files that define constants that can be accessed in other XML and Java files.
R.java
R.java is an uneditable file auto-generated by Studio, whenever we modify the XML content. This file
links the XML values to Java files. Whenever we need to use XML values in a Java file, we call these
values using the R class.
The following images show a sample XML file, where a string variable named 'title' is assigned a value
of 'Internshala - Tic Tac Toe'. This variable is then accessed in a java file using the R class.
5) Building Blocks
An Android app is built using the following components
Manifest: This file declares the app components, user permissions, app name, launcher icon etc.
Activities: An activity represents a single screen with user interface.
Services: A service is a component that runs in the background to perform long-running
operations. A service does not have a user interface
Content Providers: A content provider allows us to access a shared set of application data (for
example, contacts, music, videos etc.)
Broadcast Receivers: A broadcast receiver is a component that responds to system-wide
broadcast announcements (for example, incoming/outgoing calls/ SMSs etc.)
In this workshop, we will only cover Manifest, activities and services. You are encouraged to read all the
above components in detail after the workshop.

6) Android Manifest
The manifest file presents essential information about your app to the Android system. The system must
have this information before it can run any of the app's code.
The main functions of the manifest are:
All activities, services etc. must be declared in the Manifest.

Declaring which permissions the app has to access Android resources.


Declaring app name.
Declaring launcher icon.

7) Activities
An activity is a screen of the application with a user interface. Whenever you run an application, the first
thing you come across is a screen which is an activity. Activity provides the space for drawing UI
components with which user interacts with an app.
For example, when you run the HelloWorld app, the first screen which displays 'Hello World' is an
activity. An app must have at least one activity. When you open an app, the first activity that opens is
called the launcher activity or main activity. An application can also have multiple activities. As we have
seen before, all activities need to be specified in the manifest.xml. The launcher/main activity must be
declared with additional information.

7.1) Activity States


An activity has three states:
If an activity in the foreground of the screen (is visible to user), it is active or running.
If an activity is visible, but has lost focus (another activity has taken precedence, such as an alert
or a popup), then the current activity is inpaused state. A paused activity is completely alive, but
may get automatically killed by the system in extreme low memory situations.
If an activity is completely obscured by another activity, it is stopped. It still retains it's data.

However, it is no longer visible to the user so its window is hidden and it will often be killed by the
system when memory is needed elsewhere.
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to
finish, or simply killing its process. In this case, when it is displayed again to the user, it must be
completely restarted and restored to its previous state.

7.2) Activity Life Cycle


Whenever we create an activity, we must declare some functions (methods). These methods are
automatically called under certain circumstances. Different methods are called at different stages of the
activity's life cycle. Some of the most commonly used methods are discussed below-
onCreate(): This method is called when the activity is initializing
onStart(): This method is called once the activity has loaded completely
onPause(): This method is called when the activity is visible, but another activity is in focus
onResume(): This method is called after activity has loaded completely and when the activity has
resumed from onPause() state
onStop(): This method is called once another activity has completely obscured it and it is no
longer visible
onDestroy(): The final method called once the activity has finished/ is destroyed by the system
The following image describes the various methods in an activity life-cycle and when they are called.
Try to understand what each method does and when it is called in detail later.
7.3) How to create an activity?
To create an activity, you create a Java class that extends the Activity base class or one of its existing
subclasses, such as AppCompatActivity.
8) Services
Android service is a component that runs in the background to perform long-running operations without
needing to interact with the user and it works even if application is killed.
An application can start a service and continue running in the background even though the user switches
to another application. For example, music player keeps playing while you start chatting on whatsapp.
Another example is Internshala app's push notification which will work even if you manually kill the
app.
Service can be either started or bound.
Started: A service is started, when any application component starts it by calling startService()
method. Such services continue running in the background even if the component or the
application which started it stops performing.
Bound: A service is bound, when an activity binds itself with the service by calling bindService()
method. Such service continues operating till the activity is destroyed. In this case, even the
service gets destroyed.
8.1) How to create a service?
To create an service, you create a Java class that extends the Service base class or one of its existing
subclasses.
8.2) How to start a service?
A service is started when an an activity starts it by calling startService()/ bindService().

To stop, a service can call stopSelf() method to stop itself or another service can stop it by
calling stopService().
9) Intents & Intent Filters
Intents are used to start a new activity. They are like messengers that carry certain data about the activity
that is to be started. Intents can also be used to start a new service or to deliver a broadcast.

9.1) Components of an intent


An intent object is passed whenever a new activity is started. This object contains the information
required by the operating system to understand which action to start. An intent object, being used to start
an activity, contains following important information:
Activity name (Optional): The name of the activity to be started. This is optional; If no name is
specified, system determines how to handle it. Such an intent is called an implicit intent. Explicit
intents have the name of the activity to be started.
Action: A string specifying which type of action to perform. For example, ACTION_VIEW,
ACTION_SEND.
Data: Specifies the data to be sent to the new activity.
Extras: Additional information which is provided in key-value pairs.

9.2) Intent Filter


In an Android device, there maybe multiple apps which start different intents. We need to tell Android
which intents are to be captured by our app. This is done by creating an intent filter in the Manifest file
inside the <intent-filter> elements.
The following code will demonstrate how to add intent-filter in the manifest file and start an activity
using both implicit and explicit intents. Suppose we add an activity with intent-filter in the Manifest as
shown in the code below.

<activity android:name=".ImplicitActivitiy" android:label="@string/app_name">


<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"/>
</intent-filter>
</activity>
The following code shows how to start the activity using implicit intent.

Intent intent = new Intent(Intent.ACTION_VIEW,


Uri.parse("http://internshala.com"));
startActivity(intent);

The following code shows how to start the activity using explicit intent.

Intent intent = new Intent(this, ImplicitActivity.class);


startActivity(intent);

We have defined a filter which will capture 'http' requests. Here action defines that we want
to view something, category is default (i.e we are not defining it to particular category eg. Launcher,
Home, etc.) and in data we are adding a check to capture all required 'http' requests.

We are creating an intent with Action as Intent.ACTION_VIEW and we are passing a http uri. After
that, we are starting ImplicitActivity just by passing the above intent to startActivity() method. Since
this intent contains the same action (Intent.ACTION_VIEW) & data (http uri), so automatically our
Intent-Filter captures this and starts ImplicitActivity.

10) RepeatMyText App Links


Click here to download the apk file.
Click here to download the source code.

11) Tic Tac Toe App


Click here to download the apk file.
Click here to download the source code.

12) Requirements to become an Android developer

12.1) Java
Android is based on Java to a large extent. You should know opbject-oriented programming concepts
such as classes, objects, inheritance etc. You should also know basics such as input/output, variables,
loops etc. You should also be able to read and undertand documentation to learn how to use new parts of
the Java SDK.
12.2) The Android SDK
It includes:
Different types of layouts & views
Activities, Intents & Services
Fragments
Lifecycle of an activity
Interacting with web
Material Design
Integration with various APIs
Action Bar
Android has had several versions (Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice Cream
Sandwich, Jelly Bean, KitKat, Lollipop, Marshmallow). A developer must ensure that the APIs and
features work with the required versions.
Android devices have many different screen sizes, resolutions and pixel densities. A developer must
ensure that the app works on all of them.

12.3) Working with APIs


APIs are generally used to get data from or send data to the web. These will usually be JSON/REST
APIs. An Android developer needs to learn how to integrate these APIs with the app.

12.4) Back-end Skills (Optional but recommended)


Sometimes, an Android developer may also need to contribute to the back-end of their system. Back-end
maybe based on PHP/ Ruby/ Python etc. This may be harder to add to your skill-set when you are just
starting out and may not be required for an entry-level position.

13) Learning Java

Learning on your own:


Vogella
Tutorials Point
The New Boston (Video tutorials)

Guided online training:


Internshala VTC
Udemy
14) Learning Android

Learning on your own:


Google Developers
Vogella
The New Boston (Video tutorials)

Guided online training:


Internshala VTC
Udacity

15) Google play developer account


The account has $25 registration fee. Unlimited apps can be uploaded and managed.
https://play.google.com/apps/publish/signup/

Good luck with your projects :)

Vous aimerez peut-être aussi