Android Unit 3
Android Unit 3
Android Unit 3
Activities
Activities dictate the UI and handle the user interaction to the smartphone
screen.
An activity represents a single screen with a user interface.
For example, an email application might have one activity that shows a list of
new emails, another activity to compose an email, and another activity for
reading emails.
An activity is implemented as a subclass of Activity class as follows:
public class MainActivity extends Activity { }
Services
A service is a component that runs in the background to perform long-running
operations.
They handle background processing associated with an application
For example, a service might play music in the background while the user is in a
different application, or it might fetch data over the network without blocking
user interaction with an activity.
A service is implemented as a subclass of Service class as follows:
public class MyService extends Service { }
Broadcast Receivers
They handle communication between Android OS and applications.
Recieves and
Broadcast Receivers simply respond to broadcast messages from other
applications or from the system.
For example, applications can also initiate broadcasts to let other applications
know that some data has been downloaded to the device and is available for
them to use, so this is broadcast receiver who will intercept this
communication and will initiate appropriate action
A Broadcast Receiver in Android is a component that allows the system to
deliver events or messages to the application, even when the application is not
running. Broadcast Receivers are particularly useful for responding to system-
wide events, such as incoming SMS messages or emails.
android.provider.Telephony.SMS_RECEIVED
com.example.EMAIL_RECEIVED
public class MyReceiver extends BroadcastReceiver { }
Content Providers
A content provider component supplies data from one application to others on
request.
The data may be stored in the file system, the database or somewhere else
entirely.
They handle data and database management issues.
A content provider is implemented as a subclass of ContentProvider class and
must implement a standard set of APIs that enable other applications to
perform transaction
public class MyContentProvider extends ContentProvider { }
Fragments
A Fragment is a piece of an application's user interface or behavior that can be
placed in an Activity which enable more modular activity design. It will not be
wrong if we say, a fragment is a kind of sub-acitivity.
A fragment has its own layout and its own behavior with its own lifecycle. You
can add or remove fragments in an activity while the activity is running. You
can combine multiple fragments in a single activity to build a multi-pane UI. A
fragment can be used in multiple activities. Fragment life cycle is closely related
to the lifecycle of its host activity which means when the activity is paused, all
the fragments available in the acivity will also be stopped. A fragment can
implement a behavior that has no user interface component.
You create fragments by extending Fragment class and You can insert a
fragment into your activity layout by declaring the fragment in the activity's
layout file, as a element.
1. Layouts:
In Android, a layout defines the structure and appearance of the user interface
(UI) in an application. It specifies how different UI elements, such as buttons,
text fields, and images, are arranged on the screen. Android uses XML files to
define layoutsommon layout types include LinearLayout, RelativeLayout,
ConstraintLayout, FrameLayout, etc.
2. Intents:
Intents are a fundamental component of Android that facilitate communication
between different components, such as activities, services, and broadcast
receivers.
Intents are used for activities to start other activities, to start services, to
broadcast events, and to request actions from other components.
3. Resources:
Resources in Android refer to external elements such as images, strings,
layouts, and other assets used in an application. By storing resources
separately from the code, it becomes easier to manage and localize content.
Resources are typically placed in the "res" directory and organized into
subdirectories such as "drawable," "layout," "values," etc.
4. Manifest:
The AndroidManifest.xml file is a crucial configuration file for an Android
application. It contains essential information about the app, such as the app's
package name, activities, services, permissions, and more. The manifest file
serves as a blueprint for the Android system to understand the structure and
behavior of the application.