MAD2
MAD2
An Android application consists of several key components that work together to create a
functional mobile app. These components include Activities, Intents, Services, and Content
Providers.
1. Activities
Example:
In WhatsApp:
When developing an Android app, you can either use the automatically created
MainActivity.java or create additional activities when needed.
When we create a new Android project and choose Basic Activity, Android Studio
automatically creates:
If our app has only one screen, we do NOT need extra activities.
5. Open AndroidManifest.xml and confirm that the new activity is automatically added
inside the <application> tag:
basic code for MainActivity.java, which is automatically created when selecting a Basic
Activity in Android Studio
Intent
Types of Intents
1. Explicit Intent
● Example: Navigating from one activity to another within the same app.
2. Implicit Intent
● The system decides which app or service can handle the request.
Services
1. Foreground Service
2. Background Service
3. Bound Service
Uses of Services
A Content Provider is a component in Android that manages shared access to an app's data.
It allows different applications to read, write, and modify data securely, even if they belong
to different apps.
Data Sharing → Enables apps to share data with other apps securely.
Data Abstraction → Hides direct database access and provides a structured interface.
Centralized Data Management → Useful for managing data across multiple apps.
Contacts App → Other apps can access contacts using the ContactsProvider.
Gallery App → Apps like Instagram or WhatsApp can fetch images from the gallery.
Messaging App → SMS apps can read messages using SmsProvider.
Content Providers
A Content Provider is a component in Android that allows applications to share and manage
data securely. It provides controlled access to app data, enabling other apps to read, write,
and modify data without direct database access.
1. Data Sharing → Enables apps to share data with other apps securely.
Contacts App → Other apps can access contacts using the ContactsProvider.
Gallery App → Apps like Instagram or WhatsApp can fetch images from the gallery.
Messaging App → SMS apps can read messages using SmsProvider.
2. When an app needs to store and manage structured data using an SQLite database.
3. When an app needs to access system data such as contacts, messages, or media
files.
Android Terminologies
Android development involves various key terminologies that are essential for
understanding how Android applications work. Below are some important Android
terminologies explained in detail:
1. Activity
2. Intent
3. Services
A Service is a background component that runs without a user interface to perform long-
running tasks such as playing music, syncing data, or downloading files.
Foreground Service → Runs in the background but shows a notification (e.g., music player).
Background Service → Runs without user interaction (e.g., syncing data).
4. Content Provider
A Content Provider allows apps to share and access data in a structured way.
Used for accessing contacts, images, messages, and other system data.
Provides a standard API to read and write data securely.
5. Broadcast Receiver
A Broadcast Receiver listens for system-wide broadcast messages (e.g., battery low,
incoming call, network change).
Used for responding to system or app-specific events.
Example: Receiving an SMS notification.
6. SharedPreferences
SharedPreferences is a lightweight storage option for saving key-value pairs. It is used for
storing small amounts of persistent data like user settings.
Example: Saving a user’s login state or app preferences.
7. RecyclerView
8. ViewModel
9. WorkManager
WorkManager is a background task manager used for executing deferrable and guaranteed
tasks.
Used for scheduling tasks like file downloads, data syncing, and notifications.
10. AndroidManifest.xml
The AndroidManifest.xml file is a configuration file that defines the essential information
about the app.
Declares activities, permissions, services, broadcast receivers, and metadata.
Example: Declaring internet permissions for accessing the web.
11. Gradle
Gradle is a build automation tool used for compiling and managing dependencies in
Android projects.
It automates the build process and integrates external libraries easily.
Application Context
Application Context
Activity Context
● Used for UI-related operations like inflating layouts and launching activities.
Application Context refers to the global context of the application lifecycle. It is associated
with the application's environment and can be used when an object or component needs a
context that lasts as long as the application is running.
1. Global Scope: It remains alive throughout the entire lifecycle of the application.
3. Memory Efficient: Since it lasts as long as the app, it avoids unnecessary memory
leaks compared to Activity Context.
Uses of Application Context:
3. Accessing Shared Preferences and Databases: Used for storing and retrieving data in
SharedPreferences or SQLite databases.
From an Activity:
@Override
super.onCreate();
appContext = this;
return appContext;
Application Context is a powerful tool in Android that provides a global context for
resources, services, and background tasks. However, it should be used carefully, especially
when working with UI-related operations, as it does not have access to UI components.
Activities
An Activity in Android represents a single screen with a user interface (UI). It serves as an
entry point for user interactions with an app. Each application can have multiple activities,
and they work together to provide a seamless user experience.
An activity is a component of an Android app that provides a screen where users can
interact. It is a crucial part of the Android application lifecycle, managing the UI and
handling user inputs.
Each activity in an app is independent but can communicate with other activities to perform
different tasks.
Example:
Activity Lifecycle
Lifecycle Methods:
1. onCreate() – Called when the activity is first created. Used to initialize components.
4. onPause() – Called when the activity is partially visible but not in focus.
When developing an Android app, we can either use the automatically created
MainActivity.java or create additional activities when needed.
When we create a new Android project and choose Basic Activity, Android Studio
automatically creates:
If our app has only one screen, we do NOT need extra activities.
10. Open AndroidManifest.xml and confirm that the new activity is automatically added
inside the <application> tag:
basic code for MainActivity.java, which is automatically created when selecting a Basic
Activity in Android Studio:
MainActivity.java
package com.example.myapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
@Override
startActivity(intent);
});
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="20dp"/>
<Button
android:id="@+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second Activity"/>
</LinearLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:allowBackup="true"
android:theme="@style/Theme.MyApp"
android:usesCleartextTraffic="true"
android:supportsRtl="true"
android:label="My App">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Service
The Service component in Android follows a structured lifecycle and is divided into the
following parts:
1. Declaration in Manifest
Lifecycle of a Service
A service goes through different lifecycle stages:
Method Description
Foreground Service
Background Service
▪ Runs in the background without user interaction.
Bound Service
Start a service:
o startService(intent);
Stop a service:
o stopService(intent);
Binding to a Service
@Override
super.onCreate();
@Override
return START_STICKY;
}
@Override
return null;
@Override
super.onDestroy();
The Service component in Android is essential for running background tasks efficiently. By
managing its lifecycle properly, developers can ensure smooth operation without affecting
the app’s performance.
Intent
Types of Intents
1. Explicit Intent
startActivity(intent);
2. Implicit Intent
● Used when the target component is not specified, allowing the system to determine
which application can handle the request.
intent.setData(Uri.parse("https://www.google.com"));
startActivity(intent);
Components of an Intent
3. Category – Additional information about the type of component that should handle
the intent.
Action Purpose
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Hello, this is a test email.");
Sending Data
intent.putExtra("username", "JohnDoe");
startActivity(intent);
Intent is a powerful mechanism in Android that enables navigation and data sharing
between components. Whether you use explicit or implicit intents depends on whether you
know the target component in advance.