Filna 3
Filna 3
Filna 3
ACTIVITY
• An activity represents a single screen with a user interface just like window or
frame of java that allows users to interact with the application.
Example-Facebook app provides an activity to log into the application. There could be
other activities like posting a photograph, send a message to a friend etc
Example of an activity
First call back method which is fired when the system creates an
activity for the first time.
During the activity creation, activity enters into created state.
@override
This method will invoke when an activity enters into started state by completing the
onCreate() method.
The onStart() method will make an activity visible to the user and this method execution will
finish very quickly.
@override
protected void onStart()
{super.onStart();
}
After this method execution, activity enters into resumed state and system invokes
onResume() method.
3.onResume()
@override
public void onResume()
{super.onResume();
If(mcamera==null)
{initializeCamera();
}
}
4.onPause()
When the user leaves an activity or current activity is being paused, system
invokes onPause() method.
It is used to pause activities like stop playing the music when the activity is in
paused state
This is invoked when an activity is no longer visible to the user. this method
releases all the app resources no longer needed by the user.it can happen if
current activity entered into resumed state or newly launched activity
covers complete screen or it is destroyed.
Example
@override
protected void onStop()
{super.onStop();
}
7.onDestroy()
The system will invoke onDestroy() before an activity is destroyed and this is
the final callback method received by the android activity.
This happens when either the activity is finished or the system destroyed the
activity to save space.
@override
public void onDestroy()
{
super.onDestroy();
}
This method releases all the resources which are not released by previous
onStop() method.
• When you open the app it goes through below states
onCreate()->onStart()->onResume()
• When you press the back button and exit the app the sequence of states are as
follows
onPaused->onStop()->onDestroy()
• When you press the home button, below is the sequence of states
onPaused->onStop()
• After pressing the home button, again you open the app from a recent task list, states
will be as follows
onRestart()->onStart()->onResume()
• After dismissing the dialog or back button from the dialog the state is given below
onResume()
• If a phone is ringing and user is using the app, states are given below
onPause()->onResume()
• After the call ends the state is given below
onResume()
• When your phone screen is off below is the sequence of events
onPaused()->onStop()
• When your phone screen is turned back on, following activities occur
onRestart()->onStart()->onResume()
INTENT
• To perform an action on the screen.
• Intents are message passing mechanism to communicate an action.
Example-view a message, send a email, display a photograph, play a
video etc.
1.Starting an activity
• An Activity represents a single screen in an app. You can start a new
instance of an Activity by passing an Intent to startActivity().
• The Intent describes the activity to start and carries any necessary data.
2.Starting a service
• A Service is a component that performs operations in the background
without a user interface.
• You can start a service to perform a one-time operation (such as
downloading a file) by passing an Intent to startService().
3.Delivering a broadcast-
• A broadcast is a message that any app can receive. The system delivers
various broadcasts for system events, such as when the system boots up
or the device starts charging.
• You can deliver a broadcast to other apps by passing
an intent to sendBroadcast() or sendOrderedBroadcast().
INTENT STRUCTURE
❑ ACTION- general action to be performed such as
ACTION_VIEW,ACTION_EDIT,ACTION_MAIN etc
❑ DATA
Data to operate on such as a person record in the contact database
,expressed as a URI(uniform resource identifier).
URI (string of characters used to identify a resource)identifies a
resource either by location, or a name or both.
Explicit intents specify which application will satisfy the intent, by supplying
either the target app's package name or a fully-qualified component class
name.
For example, you might start a new activity within your app in response to a
user action, or start a service to download a file in the background.
Here the target name is not passed in the intent at the time of creating it.
Android itself decides which component of which application should
receive this intent.
For example, if you want to show the user a location on a map, you can use
an implicit intent to request that another capable app show a specified
location on a map.
Intent Filter
When you use an implicit intent, the Android system finds the appropriate
component to start by comparing the contents of the intent to the intent
filters declared in the manifest file of other apps on the device. If the intent
matches an intent filter, the system starts that component and delivers it
the Intent object.
Likewise, if you do not declare any intent filters for an activity, then it can be
started only with an explicit intent.
Attributes
syntax:
<intent-filter android:icon="drawable resource"
android:label="string resource"
android:priority="integer" >
...
</intent-filter>
• android:icon- An icon that represents the parent activity, service, or broadcast
receiver when that component is presented to the user as having the
capability described by the filter.
• android:label-
The label should be set as a reference to a string resource
android:priority -The priority that should be given to the parent
component with regard to handling intents of the type described by
the filter.
Those with higher priority values are called before those with lower
values
Fig shows how an implicit intent is delivered through the system to start another
activity:
[1] Activity A creates an Intent with an action description and passes it
to startActivity()
[2] The Android System searches all apps for an intent filter that matches the
intent.
When a match is found, [3] the system starts the matching activity (Activity B) by
invoking its onCreate() method and passing it the Intent.
Pending intent
• specifies an action to take in the future
• 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.
2.onBind()
The system calls this method when another component wants to bind with the service
by calling bindService(). If you implement this method, you must provide an interface
that clients use to communicate with the service, by returning an IBinder object.
3.onUnbind()
The system calls this method when all clients have disconnected from a particular
interface published by the service.
4.onRebind()
The system calls this method when new clients have connected to the service, after
it had previously been notified that all had disconnected in its onUnbind(Intent).
5.onCreate()
The system calls this method when the service is first created
using onStartCommand() or onBind() by a new component. This call is required to
perform one-time set-up.
6.onDestroy()
The system calls this method when the service is no longer used and is being
destroyed. Your service should implement this to clean up any resources such as
threads, registered listeners, receivers, etc.
MULTIMEDIA
The Android multimedia framework includes support for playing variety of common
media types, so that you can easily integrate audio, video and images into your
applications.
1.Linux kernel -it is the heart of android architecture that interfaces computers
hardware and its processes.
Responsible for device drivers, power management, memory management, device
management and resource access.
2.Native libraries
On the top of linux kernel,there are native libraries like WebKit,
OpenGl,FreeType,SQLite, Media,C runtime library(libc)
3.Android runtime
There are core libraries and DVM(Dalvic virtual machine) responsible to run android
application.
DVM is like JVM but optimized for mobile devices. It consumes less memory and
provides fast performance.
4.Android framework
5 Applications
• Java classes calls the native c library Libmedia through java native interface.
Code snippet, to play an audio that is available in our application’s local raw resource
(res/raw) directory.
The second parameter in create() method is the name of the song that we want to play
from our application resource directory (res/raw). In case if raw folder not exists in
your application, create a new raw folder under res directory and add a proper
encoded and formatted media files in it.
if we want to play an audio from a URI that is locally available in the
system, we need to write the code like as shown below.
Method Description
getCurrentPosition()It is used to get the current position of the song in
milliseconds.
getDuration() It is used to get the total time duration of the song in
milliseconds.
isPlaying() It returns true / false to indicate whether song playing or not.
pause() It is used to pause the song playing.
setAudioStreamType( it is used to specify the audio streaming type.
)
setDataSource() It is used to specify the path of audio / video file to play.
setVolume() It is used to adjust media player volume either up / down.
seekTo(position) It is used to move song to particular position in milliseconds.
getTrackInfo() It returns an array of track information.
start() It is used to start playing the audio/video.
stop() It is used to stop playing the audio/video.
reset() It is used to reset the MediaPlayer object.
release() It is used to releases the resources which are associated
with MediaPlayer object.
TEXT TO SPEECH IN
ANDROID
Android allows you convert your text into voice. Not only you can convert it but it also
allows you to speak text in variety of different languages.
Android provides TextToSpeech class for this purpose. In order to use this class, you
need to instantiate an object of this class and also specify the initListener.
ttobj.setLanguage(Locale.UK);
The method setLanguage takes an Locale object as parameter. The list of some of
the locales available are given below
Sr.No Locale
1 US
2 CANADA_FRENCH
3 GERMANY
4 ITALY
5 JAPAN
6 CHINA
Once you have set the language, you can call speak method of the class to speak the text.
Its syntax is given below −
ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
Some other methods available in the TextToSpeech class
1 addSpeech(String text, String filename)
This method adds a mapping between a string of text and
a sound file.
2 getLanguage()
This method returns a Locale instance describing the
language.
3 isSpeaking()
This method checks whether the TextToSpeech engine is
busy speaking.
4 setPitch(float pitch)
This method sets the speech pitch for the TextToSpeech
engine.
5 setSpeechRate(float speechRate)
This method sets the speech rate.
6 shutdown()
This method releases the resources used by the
TextToSpeech engine.
7 stop()
This method stop the speak.