Skip to main content
SERVICES
Course: Mobile Application Development
Unit : II – Building Blocks of Mobile Apps - I
Class / Semester: II MCA / III Semester
SNS COLLEGE OF TECHNOLOGY
(An Autonomous Institution)
Re-accredited by NAAC with A+ grade, Accredited by NBA(CSE, IT, ECE, EEE & Mechanical)
Approvedy by AICTE, New Delhi, Recognized by UGC, Affiliated to Anna University, Chennai
Enter your text here, or paste your text here.
ENTER TITLE
1
Enter your text here, or paste your text here.
ENTER TITLE
3
Enter your text here, or paste your text here.
ENTER TITLE
2
Enter your text here, or paste your text here.
ENTER TITLE
4
SESSION OBJECTIVES
INTRODUCTION TO SERVICES
 Service is a component, which facilitates an application to run in the background in
order to perform long-running operation tasks.
 A user-interface is not desirable as it is designed to operate long-running processes
without any user intervention
 A service can run continuously in the background even if the application is closed
or the user switches to another application
 A service is started when component calls
startService() method
 It is stopped by stopService() / stopSelf()
method
 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.
FORMS OF SERVICES
SERVICE LIFE CYCLE METHODS
onStartCom
mand()
system calls this method when another component,
such as an activity, requests that the service be
started, by calling startService()
onBind() system calls this method when another component
wants to bind with the service by calling
bindService()
onUnbind() system calls this method when all clients have
disconnected from a particular interface published
by the service
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).
onCreate() system calls this method when the service is first
created using onStartCommand() or onBind().
onDestroy() system calls this method when the service is no
longer used and is being destroyed
EXAMPLE- PLAY AUDIO
1. Create a new project, create empty activity
2. Modify strings.xml file
3. Working with the activity_main.xml file
4. Create custom service class will be created in the same directory where the MainActivity
class resides. To play music, the MediaPlayer object is used
5. To implement the services successfully on any android device, it is necessary to mention
the created service in the AndroidManifest.xml file
6. Run the Emulator
<resources>
<string name="app_name">Services_In_Android</string>
<string name="heading">Services In Android</string>
<string name="startButtonText">Start the Service</string>
<string name="stopButtonText">Stop the Service</string>
</resources>
Playing music in the background
IntentService
 Android also provides IntentService, which internally takes care of pushing a long-
running task onto a separate thread
 IntentService in Android is a base class for Services to handle asynchronous requests that
are expressed in the form of Intents when there is a demand to do so
 Whenever a client sends a request then the Service will be started and after handling
each and every Intent, the Service will be stopped
 Requests are sent by clients through Context.startService(Intent) calls to start the service.
 It handles each Intent one by one with the help of a worker thread and stops
automatically when the work is done.
 All the requests are controlled using a single worker thread so they may be sometimes
time-consuming.
USE OF IntentService
 Take care of asynchronous requests on the expression of Intent
 Allows the simplest ways of offloading “chunks” of processing from the UI thread of
the application to a remote work queue without launching an AsyncTask every time
processing is required
 Data is easily sent back to an application by broadcasting the result in the form of an
Intent object. A broadcast receiver catches the result for use within an app
 An IntentService is executed on a separate worker thread
 The service stops automatically when all the tasks are completed, eliminating the
need to make a call to stop the service
It helps to maintain the responsiveness of the main application without interfering with the users’ activity
EXAMPLE FOR IntentService
 Create app with start and stop service buttons
 Go to Java folder and create new class myService1.java, once code is added for
Oncommandstart and OnDestroy methd,
 Now, In MainActvity.java class we are going to make our two methods which are
startService() and stopService() for our two buttons for starting and stopping the
services.
 Then, we want to call our service to our Manifest file. So go to AndroidManifest.xml file
and add the service element
<service android:name=".MyService" android:exported="false"></service>
BOUND SERVICE
 Bound service is a provision in the Service to expose its functionality to other Android
components
 Relationship between a Service and the other component that wants to connect to the
Service is that of a server and client, respectively
 Bound Service acts as a server and the other app component that
intends to bind to the Service acts as a client
 There are two typical binding scenarios,
 Local binding is used if both the Service and the other component belong to the same app. It
is implemented using a Binder class
 Cross-app binding is used when the client component resides in a different app. It is
supported either by Messenger API or by Android Interface Definition Language (AIDL)
SNS COLLEGE OF TECHNOLOGY
(An Autonomous Institution)
Re-accredited by NAAC with A+ grade, Accredited by NBA(CSE, IT, ECE, EEE & Mechanical)
Approvedy by AICTE, New Delhi, Recognized by UGC, Affiliated to Anna University, Chennai
Notifications
Android Component
 A mechanism to inform a user about the occurrence of an event. An event might be a
missed call, a SMS (Short Message Service) received, completion of a download etc..
 It is categorized into
 Hardware notifications like Vibration, flashing LED light etc…
 software notifications like flashing message to a user with a icon
 It does not distract a user from the current task at hand, but still provides him or her an
option to respond to notification by opening the notification drawer at anytime, whenever
convenient
NOTIFICATIONS
Android Toast class provides a handy way to show users alerts but problem is that these
alerts are not persistent which means alert flashes on the screen for a few seconds and then
disappears
 Android provides two views for notifications –
normal view and big view
 Implementing a notification starts with
creating the template of notification, followed
by defining the navigation from the
notification, and finally triggering it.
 A template of notification is a blueprint that
defines icons, content text, content title, and
other related things
NOTIFICATIONS
ENTER TITLE
1
4 2
3
Create Notification Builder
Use NotificationCompat.Builder.build(). to set various Notification
properties like its small & large icons, title, priority etc
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(this)
Set its Notification properties using setSmallIcon(),
setContentTitle(), setContentText()
CREATE NOTIFICATIONS
Setting Notification properties
mBuilder.setSmallIcon(R.drawable.notification_ icon);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
Attach Actions
 optional part required if you want to attach an
action with the notifications.
 Action is defined by a PendingIntent by calling
appropriate method of NotificationCompat.Builder
Issue Notifications
 Pass the Notification object to the system by calling
NotificationManager.notify() to send notification
 Make sure you call NotificationCompat.Builder.build()
method on builder object before notifying it.
Enter your text here, or paste your text here. Enter your text here, or paste your text here. Enter your text
here, or paste your text here. Enter your text here, or paste your text here.
ENTER TITLE
PART 04
ENTER TITLE
EXAMPLE
REFERENCES
 Anubhav Pradhan, Anil V Deshpande, “Composing Mobile Apps using Android”,
Wiley Edition, 2014
 https://www.tutorialspoint.com/android/android_notifications.htm
 https://www.javatpoint.com/android-notification
THANK YOU
Click here to add content of the text,and briefly explain your point of
view.Click here to add content of the text,and briefly explain your point of
view.
Reporter: Zhao Meimei