Mobile Application Development Chapter 5
Mobile Application Development Chapter 5
Unit- 5
Computer Department
Intent
For example: Intent facilitate you to redirect your activity to another activity
on occurrence of any event. By calling, startActivity() you can perform this
task.
startActivity(intent);
1. Start an activity
2. Start a service
3. Deliver a broadcast
Types of Intents:
Explicit Intent:
• Explicit Intents are used to connect the application
internally.
• In Explicit we use the name of component which will be
affected by Intent. For Example: If we know class name
then we can navigate the app from One Activity to
another activity using Intent.
• Explicit Intent work internally within an application to
perform navigation and data transfer.
Implicit Intent:
In Implicit Intents we do need to specify the name of the
component. We just specify the Action which has to be
performed and further this action is handled by the
component of another application.
The basic example of implicit Intent is to open any web
page.
Intent intentObj = new Intent(Intent.ACTION_VIEW);
intentObj.setData(Uri.parse("https://www.google.com"));
startActivity(intentObj);
Intent Filters
Intent Filter are the components which decide the behavior of
an intent. Intent filters specify the type of intents that an Activity,
service or Broadcast receiver can respond to. It declares the
functionality of its parent component (i.e. activity, services or
broadcast receiver).
Syntax of Intent Filters:
Intent filter is declared inside Manifest file for an Activity.
Attributes of Intent Filter:
1. android:icon
An icon represents the activity, service or broadcast receiver when a
user interact with it or when it appears to user in an application. To
set an icon you need to give reference of drawable resource as
declared android:icon=”@drawable/icon”.
2. android:label
A label represents the title of an activity on the toolbar. You can have
different Labels for different Activities as per your requirement or
choice. The label should be set as a reference to a string resource.
Syntax of Intent Filters:
Intent filter is declared inside Manifest file for an Activity.
<activity android:name=".MainActivity">
<intent-filter android:icon="@drawable/icon"
android:label="@string/label“ >
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Elements In Intent Filter:
There are following three elements in an intent filter:
1. Action
2. Data
3. Category
1. Action:
It represent an activities action, what an activity is going to do. Action is
a string that specifies the action to perform.It is declared with the name
attribute as given below
<action android:name = "string" />
There are few common actions for starting an activity like
ACTION_VIEW
ACTION_VIEW: This is used in an Intent with startActivity(). This helps
when you redirect to see any website, photos in gallery app or an
address to view in a map app.
Intent intentObj = new Intent(Intent.ACTION_VIEW);
intentObj.setData(Uri.parse("https://www.google.com"));
startActivity(intentObj);
Elements In Intent Filter:
2. Data:
There are two forms in which you can pass the data, using
URI(Uniform Resource Identifiers like scheme, host, port, path)
or MIME type of data.
The syntax of data attribute is as follows:
<data android:scheme="string"
android:host="string"
android:port="string"
android:path="string"
android:pathPattern="string"
android:pathPrefix="string"
android:mimeType="string" />
Elements In Intent Filter:
ACTION DATA MEANING
From the multiple activities in android app, one activity can be marked as
a main activity and that is the first screen to appear when we launch the
application. In android app each activity can start another activity to
perform different actions based on our requirements. Android Activity
Lifecycle is controlled by 7 methods of android.app.Activity class.
an activity goes through a series of states during its lifetime. Android
system initiates its program within an Activity starting with a call on
onCreate() callback method.
Method Description
onResume called when activity will start interacting with the user.
<receiver android:name=".SampleBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
The above statement will fire the defined system broadcast event
Another way is to register a receiver dynamically
via Context.registerReceiver() method.
To register broadcast receiver we need to extend our class
using BroadcastReceiver and need to implement
a onReceive(Context, Intent) method like as shown below.
Method Description
Event Description
we can use content provider whenever we want to share our app data with
other apps and it allow us to make a modifications to our application data
without effecting other applications which depends on our app.
content://contacts_info/users
insert() - This method will insert a new row into our content
provider and it will return the content URI for newly inserted row.
2) Bound Service
A service is bound when another component (e.g. client)
calls bindService() method. The client can unbind the service by
calling the unbindService() method.
The service cannot be stopped until all clients unbind the
service.
onCreate()
This is the first callback which will be invoked when any component starts
the service. If the same service is called again while it is still running this
method won’t be invoked.
onStartCommand()
This callback is invoked when service is started by any component by
calling startService(). It basically indicates that the service has started and
can now run indefinetly.
onBind()
This is invoked when any component starts the service by calling onBind.
onUnbind()
This is invoked when all the clients are disconnected from the service.
onRebind()
This is invoked when new clients are connected to the service. It is called
after onUnbind
onDestroy()
This is a final clean up call from the system. This is invoked just before the
service is being destroyed. It’s very useful to cleanup any resources such
as threads, registered listeners, or receivers.
Android Multimedia framework
The android multimedia system includes multimedia applications,
multimedia framework, OpenCore engine and hardware abstract for
audio/video input/output devices. And the goal of the android multimedia
framework is to provide a consistent interface for Java services. The
multimedia framework consists of several core dynamic libraries such as
libmediajni, libmedia, libmediaplayservice and so on. A general multimedia
framework architecture is shown in Figure
MediaPlayer class
The android.media.MediaPlayer class is used to control the audio or
video files.
Methods of MediaPlayer class
There are many methods of MediaPlayer class. Some of them are:
Method Description
public void setDataSource(String path) Sets the data source (file path or http url) to use.
public void setLooping(boolean looping) Sets the player for looping or non-looping.
public void setMediaController(MediaController controller) Sets the media controller to the video view.
public void setVideoURI (Uri uri) Sets the URI of the video file.
In this listener, you have to specify the properties for TextToSpeech object ,
such as its language ,pitch e.t.c.
To start recording the audio call the two basic methods prepare() and start()
are used.
Sr.No Method & description
1 setAudioSource()
This method specifies the source of audio to be recorded
2 setVideoSource()
This method specifies the source of video to be recorded
3 setOutputFormat()
This method specifies the audio format in which audio to be stored
4 setAudioEncoder()
This method specifies the audio encoder to be used
5 setOutputFile()
This method configures the path to the file into which the recorded audio is to be stored
6 stop()
This method stops the recording process.
7 release()
This method should be called when the recorder instance is needed.
Camera In Android
In Android, Camera is a hardware device that allows capturing pictures and
videos in your applications.
Android provides the facility to work on camera by 2 ways:
1. By Camera Intent
2. By Camera API
AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA"/>
2. Using Camera By using Camera Api
This class is used for controlling device cameras. It can be used to take
pictures when you are building a camera application.
Camera API works in following ways:
1.Camera Manager: This is used to get all the cameras available in the
device like front camera back camera each having the camera id.
2.CameraDevice: You can get it from Camera Manager class by its id.
int getState() returns the current state of the local bluetooth adapter.
It combines a clean SQL interface with a very small memory footprint and
decent speed.
Methods of SQLiteOpenHelper class
There are many methods in SQLiteOpenHelper class. Some of them are as
follows:
Method Description
public abstract void onCreate(SQLiteDatabase db) called only once when database is created for
the first time.
void execSQL(String sql) executes the sql query not select query.
long insert(String table, String nullColumnHack, inserts a record on the database. The table
ContentValues values) specifies the table name, nullColumnHack
doesn't allow completely null values. If second
argument is null, android will store null values if
values are empty. The third argument specifies
the values to be stored.
Cursor query(String table, String[] columns, returns a cursor over the resultset.
String selection, String[] selectionArgs, String
groupBy, String having, String orderBy)
Opening and Closing Android SQLite Database Connection
Before performing any database operations like insert, update, delete
records in a table, first open the database connection by
calling getWritableDatabase() method as shown below: