Android Programming Notes UNIT-II
Android Programming Notes UNIT-II
(Ref: University of Babylon - College of IT – SW Dep. - Android Assist. Lect. Wadhah R. Baiee 2014)
( https://www.google.com )
The Android operating system is based on the Linux kernel, with certain architectural tweaks
by Google. The Linux kernel was chosen for a variety of reasons. Most notably, Linux is a
portable operating system that can be quickly built on a variety of hardware.
The kernel serves as a layer of abstraction between the device's software and hardware.
Consider the scenario of a camera shutter release. What occurs when you use your device's
camera button to snap a photo? The hardware instruction (pressing a button) must be
transformed to a software instruction at some point (to take a picture and store it in the
gallery).
Drivers are included in the kernel to help with this procedure. When a user pushes a button,
the instruction is sent to the kernel's related camera driver, which delivers the required
commands to the camera hardware, just as when a key is pushed on a keyboard. The drivers
in the kernel command, in basic terms, control the underlying hardware. Android's basic
functionality, including process management, memory management, security, and
networking, is managed by the Linux kernel. When it comes to security and process
management, Linux is a tried-and-true platform.
Android has built a solid basis for its ecosystem by leveraging the existing Linux open-source
OS. The underlying Linux kernel is different for each version of Android.
The kernel is the most important component of every operating system. It oversees the
computer's and hardware's functions, particularly memory and CPU time. Android was
created with a strong emphasis on security. Android as a platform provides and enforces
various measures that employ multi-layered security to protect user data on mobile devices.
The Linux kernel sits behind the Android-specific modules and frameworks. The Linux
kernel comprises code for all the numerous chip architectures and device drivers it supports,
in addition to controlling processes, memory, and power management. Cameras, Bluetooth,
Wi-Fi, flash memory, USB, and audio drivers are among them. To make the Linux kernel
more suited for smartphones, Android adds several additional capabilities to it. Low Memory
Killer, a process that checks memory status and reacts to high memory demands by killing the
least critical programs while keeping the system functioning, and wake locks, a means for
apps to inform the kernel that the device needs to stay on, are two examples.
(Ref: https://emteria.com/learn/linux-kernel )
ART and Dalvik are compatible runtimes running DEX bytecode, so apps developed for
Dalvik should work when running with ART.
Android Runtime Core Libraries Mainly consists of 3 parts:
▪ JAVA Libraries
▪ Android Libraries
▪ DVM Libraries
Standard JAVA libraries is a must alongside specific libraries for Android to control and
access media, databases etc. DVM Libraries are specifically used to interact with the Dalvik
Virtual Machine
DMV
• Uses JIT Compilation: whenever you start an application, the part of the code
necessary for app execution compiles. The rest of the code compiles dynamically. This
slows down app launch and operation but reduces the installation time.
• Speeds up the device’s startup because the application cache is created at runtime.
• DVM apps require less memory than those running on ART.
• Reduces battery performance by increasing the CPU load.
• Dalvik is “obsolete” and is not used on Android versions above 4.4.
ART
• Uses AOT compilation, i.e., compiles all code during the installation of the app. This
speeds up apps running and operation but requires more installation time.
• Slows the device’s startup because the cache is created during the first boot.
• Due to the AOT compilation approach, requires more memory compared to DVM
applications.
• Increases battery performance by reducing CPU operation due to the compilation
absence when running applications.
• Improved Garbage Collection. When using Dalvik, garbage collectors had to perform
two heap passes, which resulted in bad UX(User Experience). In the case of ART, this is
not the case: it cleans the heap once for memory consolidation.
(Ref : https://intexsoft.com/blog/android-runtime-environment-dvm-vs-art/
https://developer.android.com/guide/practices/verifying-apps-art.html )
The Dalvik Virtual Machine (DVM) is a custom virtual machine designed for running
Android applications, executing Dalvik bytecode (compiled from Java code) and optimized
for low-memory, mobile devices, and it was replaced by Android Runtime (ART) in Android
5.0 . The native code in the core libraries (chiefly dalvik/libcore, but also dalvik/vm/native)
is written in C/C++ and is expected to work without modification in a Linux environment.
Much of the code comes directly from the Apache Harmony project.The core libraries pull in
code from many other projects, including OpenSSL, zlib, and ICU. These will also need to be
ported before the VM can be used.
Core Libraries:
The Dalvik core libraries are written in C/C++ and are designed to work in a Linux
environment.
• dalvik/libcore
It is a library of core Java classes and utilities used by the Android platform . A
collection of core Java libraries and utilities that are part of the Android platform.
◦ These libraries provide fundamental functionality, such as networking, I/O, and
security, that is used by Android applications and the system itself.
◦ libcore is written in Java and is designed to be platform-independent, meaning it
can run on different hardware and operating systems.
◦ The native code in the core libraries (chiefly dalvik/libcore) is written in C/C++
and is expected to work without modification in a Linux environment.
◦ libcore is part of the Android Open Source Project (AOSP)
• dalvik/vm/native
"native" refers to code written in languages like C/C++ and executed directly by
the device's CPU.
◦ "Native" refers to code written in languages like C or C++, which are compiled
directly into machine code for the specific hardware architecture.
◦ Android applications can use native code for performance-critical tasks or to access
hardware features that aren't easily available through Java.
◦ The Java Native Interface (JNI) allows Java code to interact with native code.
External Libraries:
The core libraries also pull in code from other projects, including:
• OpenSSL
• zlib
• ICU ( International Components for Unicode )
(Ref : www.google.com)
These are tools and frameworks that allow Java applications to interact with code or systems
written in other languages or technologies. Java Interoperability Libraries facilitate
communication and interaction between Java code and other languages or systems, enabling
the use of existing libraries and frameworks in a Java environment or vice versa. They enable
Java to leverage existing libraries and frameworks, enhance system integration, and take
advantage of specific language or platform advantages.
Benefits:
Examples
• Java Native Interface (JNI): Allows Java code to call native methods (written in
languages like C or C++) and vice versa.
• GraalVM: Enables JavaScript code to access Java libraries and services directly.
• Kotlin: Kotlin, a JVM language, is designed with Java interoperability in mind,
allowing seamless integration of Kotlin and Java code.
• Other Interoperability Efforts: Libraries like Swift-Java are being developed to
facilitate interoperability between Java and Swift.
(Ref : https://justacademy.co/blog-detail/java-interoperability )
Android Libraries
Ans.
The Android Library provides the
device with a set of instructions
that allow it to handle different
types of data in an appropriate way.
As part of Android development,
native libraries are required, and
most of these libraries are open-
source. It is a collection of C/C++
core libraries as well as Java-based
libraries, which support Android development, such as Graphics, Libc, SSL (Secure Socket
Layer), SQLite, Media, Webkit, OpenGL (Open Graphic Library), Surface Manager, etc. Here
are some details about some key Android libraries that are available for Android
development..
Surface Manager - manages access to the display subsystem and seamlessly composites 2D
and 3D graphic layers from multiple applications.
Android multimedia framework is designed to provide a reliable interface for java service. It
is a system that includes multimedia applications, frameworks, an OpenCore engine, hardware
devices for audio/video/ input, output devices also several core dynamic libraries such as
libmedia, libmedia playservices, and so on.
OpenGL ES (OpenGL for Embedded Systems) is a graphics API used on Android devices,
allowing developers to render high-quality 2D and 3D graphics on mobile devices by leveraging
the power of the device's GPU, essentially providing a way to create visually rich applications
like games and animations within the Android platform.
FreeType is a software development library used to render text onto bitmaps, and which
provides support for other font-related operations. The FreeType font rasterization engine is free
and open-source software with the source code dual-licensed under a BSD-like license and the
GPL
WebKit refers to an open-source web browser engine used to render web pages within
applications, primarily through the "WebView" component; essentially, it's the underlying
technology that allows Android apps to display web content within their interface, similar to
how Apple's Safari browser utilizes WebKit on iOS devices.
SGL stands for "Scalable Graphics Library," which is a 2D graphics library used alongside OpenGL (for
3D graphics) to render graphics within the Android system; essentially, it provides a way to draw 2D
images on the screen within Android applications.
SSL stands for "Secure Sockets Layer," which is a security protocol used to encrypt data
transmitted between an Android app and a server, ensuring that sensitive information like login
credentials or personal details remain private and protected from eavesdropping during
communication over the internet; essentially, it's a way to establish a secure connection between
your app and a website or server by encrypting the data being sent and received.
Libc is part of Native C/C++ Libraries which provide an important functions that could be used by the
application to interact with the operating system.
(Ref: https://www.interviewbit.com/blog/android-architecture/)
Application Framework
Ans.
The application framework that
stands on top of the native libraries
and runtime layer provides us with
Application Programming
Interfaces (APIs) and higher-level
services. The Android Application
Framework provides classes, interfaces, and utilities that are used for the development of
Android applications. Also included in this framework is an Android Hardware Abstraction
Layer (HAL), which allows the application to communicate with hardware-specific device
drivers and to manage both the UI and resources. The overall aim of it is to provide services
through which we can create a particular class and make that particular class available for aid
when creating applications. The Android framework includes the following high-level
services that can be beneficial for developing mobile applications according to our
prerequisites:
• Activity Manager: Plays a crucial role in bringing all aspects of the applications
lifecycle and activity stack under control.
• Content Providers: These providers enable applications to publish data online and
share it with other applications.
• Resource Manager: Assists in accessing non-code embedded resources such as strings,
color settings, and layouts for user interfaces.
• Notifications Manager: Allows applications to show alerts and notifications to users
via the user interface of the application.
• View System: It serves as a base class for widgets and handles events.
(Ref: https://www.interviewbit.com/blog/android-architecture/)
Creating a New Android Project
Ans.
Creating the Project:
Open Eclipse: Launch the Eclipse IDE.
File > New > Project: Navigate to the "File" menu
and select "New" > "Project".
Select Android Project: In the "New Project"
dialog, expand the "Android" folder and select
"Android Project".
Next: Click "Next" to proceed to the project
creation wizard.
Project Details:
Project Name: Enter the name for
your Android project.
Location: Choose the directory
where you want to store the project.
Build Target: Select the target
Android SDK version for your project.
Application Name: Enter the name
of your application.
Package Name: Enter the package
name for your application.
Create Activity: Choose to create a
new activity or leave it unchecked.
Next: Click "Next" to proceed to the next
step.
Activity Template (Optional): If you
chose to create an activity, select an
activity template (e.g., Blank Activity).
Finish: Click "Finish" to create the Android project.
(Ref : https://www.google.com/ )
In the Eclipse SDK, the project name is a crucial identifier used to organize and manage
software development projects within the Eclipse IDE, serving as the basis for project
organization, file structure, and build configurations.
Project Organization:
The project name is used to create a dedicated directory structure within the Eclipse
workspace, which holds all the project's files, source code, and build artifacts.
Build Configuration:
The project name is used to identify the project when configuring build processes, such as
compiling, packaging, and deploying applications.
Dependency Management:
Project names are used to establish dependencies between different projects within a
workspace, allowing for modular development and reuse of code.
Eclipse SDK Naming Conventions:
When developing plug-ins for the Eclipse project itself, the name of the Eclipse workspace
project should match the name of the plug-in.
Project Description File:
The project name is stored in the .project file, which is a crucial part of the Eclipse workspace
API, ensuring that projects can be recreated and managed correctly in different workspaces.
Source Code Management:
The project name is often used in conjunction with source control systems (like Git) to
manage the project's code base and track changes.
SDK settings, particularly for platforms like Android, are crucial because they tell the IDE
where to find the necessary tools, libraries, and APIs for building and running applications
targeting those platforms. Without proper SDK configuration, Eclipse won't be able to
compile, debug, or deploy your code.
Here's a more detailed breakdown:
Compilation: Eclipse uses the SDK tools to compile your code into a runnable format.
Debugging: The SDK provides debugging tools that allow you to step through your code and
identify issues.
Deployment: The SDK contains the necessary tools for deploying your application to a target
device or emulator.
API Access: SDK settings ensure that Eclipse can access the necessary APIs for the platform
you're developing for.
Android SDK:
Install the Android Development Tools (ADT) plugin in Eclipse.
Navigate to Window > Preferences > Android and specify the location of your Android SDK
directory. Use the Android SDK Manager to download and install the necessary SDK
platforms and tools.
( Ref: https://www.google.com/)
Project Configuration Settings
Ans.
To configure your Android project in Eclipse, right-click the project in the Project Explorer,
select "Properties", and then navigate to the specific settings you want to modify, like
"Android"
Access Project Properties:
• In the Eclipse Project Explorer, locate the Android project you want to configure.
• Right-click on the project name.
• Select "Properties" from the context menu.
Modify Settings:
Once you've navigated to the desired settings, you can modify the values as needed.
For example, you might change the target SDK version, add a new library, or modify the
project's source folder.
Click "OK" or "Apply" to save the changes.
Android SDK: Ensure you have the correct Android SDK installed and selected for your
project.
Target SDK: Choose the Android API level your application is designed for.
Minimum SDK: Set the lowest Android API level your application will support.
Application Package Name: Specify the unique identifier for your application.
Dependencies: Add or remove libraries your project needs to function.
Build Path: Configure where your source code and resources are located.
Build Configuration: Set up different build configurations for different scenarios, like debug
and release builds.
( Ref: https://www.google.com/)
Function well in the Launcher - The launcher is where users will interact with the icon most
frequently. A successful app launcher icon will look great in all situations: on any background
and next to any other icons and app widgets. To do this, icons should:
(Ref:https://devdoc.net/android/Android-r15/guide/practices/ui_guidelines/icon_design_launcher.html)
Creating an Activity
Ans.
an Activity is a fundamental building block that
represents a single screen or a user interface with
which the user interacts. It serves as the entry point
for an app's interaction with the user, managing the
UI(User Interface) and user interactions.
Types of Activities:
Activity Lifecycle:
onCreate(): Called when the activity is first created, responsible for initializing the UI and
setting up the layout.
onStart(): Called when the activity becomes visible to the user.
onResume(): Called when the activity is about to start interacting with the user, capturing user
input.
onPause(): Called when the activity is losing focus, typically when another activity is starting
or the user navigates away.
onStop(): Called when the activity is no longer visible to the user and is in the background.
onDestroy(): Called when the activity is being destroyed, typically when the user navigates
away and the activity is no longer needed.
onRestart(): Called when the activity is being restarted after being stopped.
onSaveInstanceState(): Called before the activity is destroyed to save any state that needs to
be restored later.
onRestoreInstanceState(): Called after the activity is recreated to restore the saved state.
Activity States:
Running: The activity is at the top of the activity stack and is interacting with the user.
Paused: The activity is partially hidden and does not receive user input.
Stopped: The activity is completely hidden and is not visible to the user.
Destroyed: The activity is no longer running and has been removed from the activity stack.
Role of an Activity:
Lifecycle Management:
Activities go through a lifecycle, with various callback methods (like onCreate(),
onResume(), onPause(), etc.) that are triggered at different points in the activity's existence.
These callbacks allow you to control how your activity behaves when it's created, becomes
visible, goes into the background, or is destroyed.
Managed in a Stack:
Android manages Activities in a stack, meaning when you start a new Activity, it's placed on
top of the existing one. The user can navigate back through the stack using the back button,
returning to the previous Activities.
Using Intents:
Activities can communicate with each other (and other Android components) using Intents,
which are essentially messages that specify an action to be performed. For example, you can
use an Intent to start a new Activity, send data between Activities, or trigger an action in
another app.
Layout Files:
Activities typically use XML layout files to define their UI, allowing for a separation of
concerns between the UI and the code that handles user interactions.
Manifest Declaration:
Every Activity must be declared in the AndroidManifest.xml file, which tells the Android
system which Activities are part of your application
(Ref: https://www.google.com/ )
Prerequisites:
• Android SDK and ADT Plugin: Make sure you have the Android SDK installed and
the ADT plugin configured within Eclipse.
• AVD Configuration:Create an AVD using the Android Virtual Device Manager
(Window > AVD Manager).
• Project Setup:Ensure your Android project is properly set up with the necessary SDK
and build targets.
Running the Application:
• Select AVD: In the Eclipse toolbar, locate the dropdown menu that allows you to select
the target device (AVD or physical device).
• Choose AVD: Select the AVD you want to use from the dropdown menu.
• Run Application: Click the "Run" button (or select Run > Run from the menu).
• Emulator Launch: Eclipse will compile your project, install the application on the
selected AVD, and launch the emulator.
• Troubleshooting: If you encounter issues, consult the Android documentation for
troubleshooting tips.
(Ref: https://www.google.com/ )
Stopping a Running Application
Ans.
To stop an application running in an AVD (Android Virtual Device) within Android Eclipse,
you can either click the "Stop" button in the Debug perspective or use the "Terminate" option
in the Debug view.
(Ref: https://www.google.com/ )
To review and edit layouts in Android Eclipse, navigate to the res/layout directory in the
Package Explorer, double-click a layout file, and then use the "Design" or "Source" tabs to
view and modify the UI visually or through XML code, respectively.
Accessing the Layout Files:
Navigate to the res/layout directory: In the Eclipse Package Explorer, locate your Android
project and navigate to the res folder, then the layout sub-folder.
Open a Layout File: Double-click on the XML file representing the layout you want to review
(e.g., activity_main.xml).
Viewing and Editing the Layout:
Design Tab: The "Design" tab provides a visual representation of the layout, allowing you to
drag and drop UI elements and adjust their positions and sizes.
You can preview the layout on different screen sizes and orientations within the design view.
To review resource files in Android Eclipse, navigate to the "res" folder within your project's
Package Explorer and double-click any resource file to open it in an Android-specific view,
rather than raw code.
Locate the Resource Files: In Eclipse's Package Explorer, your project structure should show
a folder named "res" (for resources).
Open Resource Files: Double-click on any resource file within the "res" folder to open it in an
Android-specific view.
Accessing Resources in Code:
You can access resources in your Java code using the R class, which is automatically
generated by Eclipse. For example, to access a string resource named "hello", you would use
R.string.hello.
Accessing Resources in XML:
In XML layouts, you can reference resources using the @ symbol followed by the resource
type and resource name (e.g., @string/hello).
( https://www.google.com )