Android Core Building Blocks
Android Core Building Blocks :
An
Android Component is a simply piece of code that has well defined life cycle.
Android Manifest.xml File :
manifest file which
contains the description of each component and how they interact. The
manifest file also contains the app’s metadata, its hardware configuration,
and platform requirements, external libraries, and required permissions.
The basic components of any android application :
- Activities
- Intent and broadcast receivers
- Services
- Content Providers
- Widgets and Notifications
1) Activities :
Whenever we open an
Android application, then you see some UI drawn over our screen. That
screen is called an Activity.
Activities are
said to be the presentation layer of our applications.An activity is a class
that represents a single screen.
For example, when we open our Gmail
application, then we see our emails on the screen. Those emails are present in
an Activity. If we open some particular email, then that email will be opened
in some other Activity.
When we all started with coding, we know about the main method from where the program begins execution. Similarly, in Android, Activity is the one from where the Android Application starts its process. Activity is one screen of the app's user interface. There is a series of methods that run in an activity.
There is a lifecycle associated with every Activity and to make an error-free Android application, we have to understand the lifecycle of Activity and write the code accordingly.
2) Intent and broadcast receivers :
Intent
: Android Intents are the communication
medium. ex. app
components send messages to one another like you do with your friends. An Intent is a messaging object you can use to request
an action from another app component.
Intent
is used to invoke components. It is mainly used to:
- Start the service
- Launch an activity
- Display a web page
- Display a list of contacts
- Broadcast a message
- Dial a phone call etc.
Intents
are of two types:
1) Implicit Intents
2) Explicit intents
Implicit Intents :Here, you don’t need to specify the fully-qualified address. All you need to do is just specify the action that is to be performed by an Intent. By using the Implicit Intents you can communicate between various applications present in the mobile device.
For example, you can access the current location by accessing the location data from other application.In implicit intent we have to pass an action using setAction() as shown below example.
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("www.tutorialspoint.com"));
startActivity(i);In the above example we are giving action as view. so it going to show something which we have given in setData method.
Explicit intents :If you want communication between the components of your application only then you can use the Explicit Intents. Explicit Intents are used to communicate with a particular component of the same application. you can use Explicit Intents to have communication in your own application.
For example, if you want to launch an Activity by clicking some button on the present Activity then you can specify the fully-qualified address of the desired Activity to launch that Activity.
To start new activity we have to create Intent object and pass source activity and destination activity as shown below.
Intent send = new Intent(MainActivity.this, SecondActivity.class);
startActivity(send);In the above figure, you can find that if you call an Explicit Intent i.e. you are passing the fully-qualified address of the action to be performed then, the system will directly launch that activity or will start performing the desired activity.
If you call the Implicit Intent then, the Android System will search for all
the available components that can be used to start that activity.
This process is done by comparing the contents
of the intent with the content present in the intent-filters declared
in the AndroidManifest.xml file. If there is only one
intent-filter that is compatible with the content of the intent then the
Android system will start the desired component. But if there are a number
of intent-filters that are compatible with the content of the
Intent then the Android System will show you a list of application that can be
used to perform that particular action.
For example, if you want to share some image from the Gallery, then you will get a number of choices like WhatsApp, Facebook, Instagram, Shareit, Gmail and many more image sharing application. Now, you can choose any of the available choices.
Intent Filters are expressions that are used to specify the type of components
or actions that can be received by the application and this Intent Filter is
declared in the AndroiManifest.xml file. If you are not
declaring any Intent Filters, then you have to use the Explicit Intents only.
Broadcast
Receivers : A broadcast receiver is a Passive component of
the Android system.Only an Intent (for which it is registered) can bring it
into action. The Broadcast Receiver’s job is to pass a notification to the
user,in case a specific event occurs.
Using
a Broadcast Receiver, applications can register for a particular event. Once
the event occurs, the system will notify all the registered applications.
For instance, a Broadcast receiver
triggers battery Low notification that you see on your mobile screen.
Other instances caused by a Broadcast Receiver
are new friend notifications, new friend feeds, new message etc.
on your Facebook app.
In fact, you see broadcast receivers at work
all the time. Notifications like incoming messages, WiFi
Activated/Deactivated message etc. are all real-time announcements
of what is happening in the Android system and the applications.
There are two types of broadcasts :
1) Ordered Broadcasts:
2) Normal Broadcasts:
2) Normal Broadcasts: Normal broadcasts are not orderly. Therefore, the registered receivers often run all at the same time. This is very efficient, but the Receivers are unable to utilize the results.
Sometimes to avoid system overload, the system delivers the broadcasts one at a time, even in case of normal broadcasts. However, the receivers still cannot use the results.
Difference between Activity Intent and Broadcasting Intent :
You must
remember that Broadcasting Intents are different from the Intents used to start
an Activity or a Service . The intent used to start an Activity makes changes
to an operation the user is interacting with, so the user is aware of the
process. However, in case of broadcasting intent, the operation runs completely
in the background, and is therefore invisible to the user.
3)
Services :
Android service is a component that
is used to perform
operations on the background such as playing music, handle network
transactions, interacting content providers etc. It doesn't has any UI (user
interface).
The
service runs in the background indefinitely even if application is destroyed.
Example
of service in android that plays an audio in the background. Audio will not be
stopped even if you switch to another activity. To stop the audio, you need to
stop the service.
A service have two types :
1) Started
2) Bound
Started: After a service starts, it can run indefinitely and
usually performs single operation. No result is returned to user. For example,
uploading a file. After the task is completed, it should terminate itself.
Bound: In this case, a component is bound to a service so that a
particular task can be completed. This type of service provides a client-server
like interface. Requests can be send, receive requests, and return result to
the user. Inter process communication is achieved through this service. App
component can bind to a service. Multiple components can be bound to this type
of service. After the destruction of component, service terminates.
4) Content Provider :
Content
Providers are used to share data between the applications.
A content provider component supplies data
from one application to others on request. Such requests are handled by the
methods of the ContentResolver class. A content provider can use different ways
to store its data and the data can be stored in a database, in files, or even
over a network.
A content provider behaves very much like
a database where you can query it, edit its content, as well as add or delete
content using insert(), update(), delete(), and query() methods. In most cases
this data is stored in an SQlite database.
5)
Widgets and Notifications :
Notification : As the name says keeps the user aware of
events going on. User is kept informed like any news channel. For e.g
everyone of us know about facebook or whatsapp, now notification system of app
is responsible for informing you about any new friend request, chat request, or
a new message from say, dvs or xyz, etc.
android widgets : with simplified examples such as
Button,EditText,AutoCompleteTextView, ToggleButton, DatePicker, TimePicker,
ProgressBar etc. A widget is a small gadget or control of your android application placed on the home screen.
6) View : A view is the UI element such as button, label, text field etc.
Anything that you see is a view.
7) Fragment : Fragments are like parts of activity. An activity
can display one or more fragments on the screen at the same time.
8) Android Virtual Device (AVD) : It is used to test the android application
without the need for mobile or tablet etc. It can be created in different
configurations to emulate different types of real devices.
Hope you learned something new today.
Have a look at our Android Tutorials.
Do share this Blog with your fellow developers to spread the knowledge. You can read more blogs on Android on our blogging website.
Happy Learning :)
----------------------------------------------------------------------------------- Share this blog to spread the knowledge




Comments
Post a Comment