Unit - Ii Building Blocks of Android: Android Building Blocks
Unit - Ii Building Blocks of Android: Android Building Blocks
Unit - Ii Building Blocks of Android: Android Building Blocks
2. INTENDS
Intents are messages that are sent among the major building blocks. Otherwise, Intents are
asynchronous messages which allow application components to request functionality from other
Android components. Intents allow you to interact with components from the same applications
as well as with components contributed by other applications. For example, an activity can start
an external activity for taking a picture. They trigger to
START AN ACTIVITY
TO START A SERVICE
TO STOP A SERVICE
AND TO BIND A SERVICE
AND BROADCASTS
AND START AN ACTIVITY FROM ANOTHER ACTIVITY
DISPLAY A WEB PAGE
DISPLAY A LIST OF CONTACTS
DIAL A PHONE CALL
Example code to start an activity via intend:
Intent i = new Intent(this, ActivityTwo.class);
startActivity(i)
Types of Indents:
There are two types of indents available for communication purpose.
1. Explicit – Specifically tells about the receiver component
2. Implicit – Only the type of receiver will be specified
Implicit Intent:
In an implicit intent, the sender specifies just the type of receiver. Implicit Intent doesn't specify
the component directly. In implicit indent the activity name will not be specified. In such case,
this implicit intent provides information of available components that are to be invoked provided
by the system. Implicit intents specify the action which should be performed and optionally data
which provides content for the action.
If an implicit intent is sent to the Android system, it searches for all components which
are registered for the specific action and the fitting data type. If only one component is found,
Android starts this component directly. If several components are identified by the Android
system, the user will get a selection dialog and can decide which component should be used for
the intent.
public void onClick(View arg0) {
String url=editText1.getText().toString();
Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);
}
Explicit Intent:
In an explicit intent, the sender clearly spells out which specific component should be on
the receiving end.
Explicit intents explicitly define the component which should be called by the Android
system, by using the Java class as identifier. Explicit intents are typically used within an
application as the classes in an application are controlled by the application developer. The
following code shows how to create an explicit intent and send it to the Android system to start
an activity.
Intent i = new Intent(Mainactivity.this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
Indents allow to replace the app with a custom one. For example, default browsers in a
device can be replaced with custom one. Like a web link, an intent can be internal to a single
app or, just as easily, connect to components in other apps. They can carry small amounts of
primitive data with them
The communication indents for activities are shown here
3. SERVICES:
Services are the one that run in the background and don’t have any user interface
components. They can perform the same actions as activities, but without any user interface.
The service runs in the background indefinitely even if application is destroyed. Services are
useful for actions that the user want to perform for a while without worrying about the contents
on the screen. Services are used for repetitive and potentially long running operations.
Example: 1. Music player playing the music. 2. Applications that are being downloaded
while the user uses some other application.
Services Life Cycle:
Like Activity, services are also have their own life cycle. But not like activity they have
all the states. Some of the states of the activities are not undergone by service.
The states of the services are as follows. 1. Start 2. Running 3. Destroyed.
The services life cycle diagram is depicted below.
Similarly, to stop a service, the following code should be used. stopService() or stopself()
method.
OnCreate():
The system invokes this method to perform one-time setup procedures when the service
is initially created before it calls either onStartCommand() or onBind(). If the service is already
running, this method is not called.
OnStartCommand():
The system invokes this method by calling startService() when another component
such as an activity requests that the service be started. When this method executes, the service is
started and can run in the background indefinitely. When the service is started it should be
stopped after some time by calling either by calling stopSelf() or stopService() method.
OnDestroy();
The system invokes this method when the service is no longer used and is being
destroyed. The service should implement this to clean up any resources such as threads,
registered listeners, or receivers. This is the last call that the service receives.
OnBind();
The system invokes this method by calling bindService() when another component
wants to bind with the service. While implementing this method, an interface must be provided
that clients use to communicate with the service by returning an IBinder.
Sample Code: (MainActivity.java)
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
stopService(new Intent(this, MyService.class));
break;
case R.id.buttonNext:
Intent intent=new Intent(this,NextPage.class);
startActivity(intent);
break;
}
Myservice.class
4. CONTENT PROVIDERS:
Content providers are interfaces for sharing data between applications. A content
provider manages access to a central repository of data. A provider is part of an Android
application, which often provides its own UI for working with the data. Content providers can
help an application manage access to data stored by itself, stored by other apps, and provide a
way to share data with other apps. They encapsulate the data, and provide mechanisms for
defining data security. Content providers are the standard interface that connects data in one
process with code running in another process. Implementing a content provider has many
advantages. Most importantly you can configure a content provider to allow other applications to
securely access and modify the app data. Content provider
Overview diagram of how content providers manage access to storage.
Content provider are much better suited for sharing persistent data between possibly large
datasets. The methods that content providers use to implement the four critical operations are:
1. Create - insert() 4. Delete - delete()
2. Read - query()
3. Update - update()
Advantages of Content Provider:
5. BROADCAST RECEIVERS:
Broadcast Receiver is defined as a system built publish or subscription mechanism. This
is a code that gets activated by the occurrence of an event.
Example. 1. An incoming call event 2. An Incoming SMS alert 3. Low Battery alert
These events will be broadcasted and the receivers will receive them. They do not have any
visual representation. They will not run in memory actively. But when triggered, they initiate
to execute some code. Example: Starting an activity, starting a service, etc.
6. APPLICATION CONTEXT:
Application context are the containers that hold the building blocks that are in Android.
Container does not do anything but the activities, services, providers and receivers do the work
along with the filesystems, VM etc., Application context refers to the application environment
and the process within which all its components are running. It allows applications to share the
data and resources between various building blocks. It has its own Linux user ID and its own.
Linux process, with a dedicated Dalvik virtual machine, a dedicated filesystem for storing
application files, and so on.
Application Context
The application context is uniquely identified on a device based on the package name of that
application. An application context gets created whenever the first component of this application
starts up. The application context lives as long as your application is alive. The application
context lives as long as your application is alive.
User Interface is defined as the screens or the medium that provides the facility to access
the application by means of touching, dragging and clicking etc.
In declarative user interface buttons and other components can be created using XML code.
In Programmatic user interface creation, declaration of buttons, declaration of text boxes, text
view controls can be created using java code. These can be posted into a container.
Declarative User Interface
1. This is like HTML where the user interface can be created using XML Code. Built in
tags are available in android to facilitate the creation of the user interface using XML.
All the controls have their own properties such as height, width, margin, text etc are
readily available to design the interface.
2. Provides WYSIWYG
The declarative interface is based on WYSIWYG means What You See Is What
You Get. The user interface which is created in the layout will be running in the
emulator as it is. The screen color, the height, width and match parent properties will be
adopted according to the device screen accurately. That is, the layout design will be
shown on the device as it is created using XML code. Some of the tools come with ADT
and some with third parties
Output:
Disadvantages of DUI:
Why PUI?
XML is unable to provide a good method to handle the user input in a proper way.
Therefore, PUI has been introduced. In this interface creation, the user has to write the java code
in the Mainactivity. Java file to create the screen. All the layouts properties, components
properties such as height, width, color, text must be given using the corresponding methods.
This approach is involving writing java code to develop UI. Buttons can be created, in
declarative approach also, but the only difference in PUI, java informs the user that what
happens when button is clicked. But in this method, the user can see the design of the screen
only when the device / emulator runs. Any changes while designing cannot be done in this
approach. Memory space will be occupied only at run time.
Example:
Output:
Layout
Types of Layouts:
Linear Layout
Table Layout:
The components are arranged in the form of a row and column like a table on the screen
Frame Layout –
The components are arranged inside a Frame like arrangement is called Frame Layout.
Relative Layout:
The components are arranged in such a way that each component is relatively placed in a
position with the other component. If there is any change in one component, then there may be
changes in the position of the other component also.
Multithreaded Execution
While multithreading in android, the main thread as running in the foreground and the
additional threads are running in the background.
They are running but equally.
•
There are several ways to accomplish multithreading. Java has a Thread class that
allows for many of these operations
• Java can be used to put the processes into foregraound or background based on the
user’s requirement
The drawback:
• in Android, a thread that didn’t create the UI widget is not allowed to update the
UI.
• For this purpose, threads should be synchronized.
• To synchronize android provides a utility class called AsyncTask has been designed
for this purpose
AsyncTask
• AsyncTask is an Android mechanism created to help handle long operations that
need to report to the UI thread.
The methods associated with this classes are,
• doInBackground(),
• onProgressUpdate(),
• and onPostExecute().
UNIT II COMPLETED