Module - 1
Module - 1
Module - 1
PROGRAMMING
Module 1
APPLICATIONS COMPONENTS
1. Activity
2. Services
3. Broadcast Receivers
4. Content Providers
5. Intents
ACTIVITY
• An activity is an application component that provides a screen with
which users can interact in order to do something such as dial the
phone, send an email or view a map.
• Each activity is given a window in which to draw its user interface
• An application usually consists of multiple activities that are loosely
bound to each other.
• Each time a new activity starts, the previously activity is stopped,
but the system preserves the activity in a stack
• when a new activity starts, it is pushed onto the back stack and
takes user focus.
Activity Life Cycle
1. onCreate()
This is the first callback and called when the activity is
first created.
2. onStart()
This callback is called when the activity becomes
visible to the user.
3. onResume()
This is called when the user starts interacting with the
application.
4. onPause()
The paused activity does not receive user input and
cannot execute any code and called when the
current activity is being paused and the previous
activity is being resumed.
5. onStop()
6. onDestroy()
7. onRestart()
This callback is called when the activity restarts after stopping it.
SERVICES
• A 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 destroyed.
• Services doesn’t have User Interface.
• 3 types of services:
1. Foreground service
2. Background service
3. Bound service
1. FOREGROUND SERVICE
audio track.
• MULTIPLE COMPONENTS CAN BIND TO THE SERVICE AT ONCE, BUT WHEN ALL OF
THEM UNBIND, THE SERVICE IS DESTROYED.
A service can essen ally take two states −
Started
A service is started when an application component, such as an activity,
starts it by calling startService().
Once started, a service can run in the background indefinitely, even if the
component that started it is destroyed.
Bound
A service is bound when an application component binds to it by calling
bindService().
This is the best option if you don't require that your service handle multiple requests
simultaneously.
airplane mode.
android.intent.action.AIRPLANE_MODE).
• The intent may also include additional information bundled into its
extra field. For example, the airplane mode intent includes a
● android.intent.action.BATTERY_CHANGED
Sticky broadcast containing the charging state, level, and other information about the
battery.
● android.intent.action.BATTERY_LOW
● android.intent.action.BATTERY_OKAY
● android.intent.action.BOOT_COMPLETED
● android.intent.action.CALL
● android.intent.action.CALL_BUTTON
● android.intent.action.REBOOT
1. Implicit intent
2. Explicit intent
1. IMPLICIT INTENT
startActivity(intent);
2. EXPLICIT INTENT
1. <action>,
2. <category>
3. <data>
action
Uses the android:name attribute to specify the name of the action being
serviced. Each Intent Filter must have at least one action tag. Actions
should be unique strings that are self-describing. Best practice is to use a
naming system based on the Java package naming conventions.
ACTION_SEND: You should use this in intent with startActivity() when you
have some data that the user can share through another app, such as an
email app or social sharing app.
category
Uses the android:name attribute to specify under which circumstances
the action should be serviced. Each Intent Filter tag can include multiple
category tags. You can specify your own categories or use the following
standard values provided by Android:
The data tag enables you to specify which data types your component can act
on; you can include several data tags as appropriate. You can use any
combination of the following attributes to specify the data your component
supports:
uses-feature
Android is available on a wide variety of hardware platforms. Use multiple
uses-feature nodes to specify which hardware features your application
requires. This prevents your application from being installed on a device that
does not include a required piece of hardware, such as NFC hardware, as
follows:
uses-permission
As part of the security model, uses-permission tags declare the user
permissions your application requires. Each permission you specify will be
presented to the user before the application is installed. Permissions are
required for many APIs and method calls, generally those with an associated
cost or security implication (such as dialing, receiving SMS, or using the
location-based services).
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). During
development you should include a debuggable attribute set to true to enable
debugging, then be sure to disable it for your release builds.
activity
The Activity sub-element of an application refers to an activity that needs to be
specified in the AndroidManifest.xml file. It has various characteristics, like label,
name, theme, launchMode, and others. In the manifest file, all elements must be
represented by <activity>. Any activity that is not declared there won’t run and
won’t be visible to the system. It is contained within the <application> element.
<activity
android:name=".MainActivity"
android:exported="true">
</activity>