Chapter 3
Chapter 3
▪ Introduction to Intents
▪ Types of Intents
▪ Introduction Service
▪ Types of service
▪ Characteristics of Mobile Applications
▪ Chapter summery
Introduction
• The Internet is a collection of computers around the world connected to each
other via a high speed series of networks. The Internet becomes the main
method in exchanging cultures and transferring knowledge between people.
• All connected computers and networks exchange information and use various
services. Hence, the Internet is not the World Wide Web (WWW or W3).
▪ This explicit Intent work internally within an application to perform navigation and
data transfer.
• In case, if multiple intent filters are matched then the system displays a dialog so
that the user can pick which app to use.
• Example to send data from the current activity to the other activity in the same
application.
Intent intent_var = new Intent(this, secondClass.class);
Intent_var.putExtra(“Data_Name", “Data_value");
startActivity(intent_var);
//startService(intent);
Cont.…
• startService() – The Intent object is passed to this method to initiate a service or
deliver new instructions to an ongoing service/to communicate with a background
Service.
}
Services
}
Next class Types of Services
These are the three different types of services:
Foreground: A foreground service performs some operation that is noticeable to the
user.
✓ For example, an audio app would use a foreground service to play an audio track.
✓Foreground services must display a Notification. Foreground services continue
running even when the user isn't interacting with the app.
✓ When you use a foreground service, you must display a notification so that users
are actively aware that the service is running.
✓This notification cannot be dismissed unless the service is either stopped or
removed from the foreground.
Cont.…
• Note: The Work Manager API offers a flexible way of scheduling tasks, and is able to run
these jobs as foreground services if needed.
• In many cases, using Work Manager is preferable to using foreground services directly.
• Background: A background service performs an operation that isn't directly noticed by the
user.
• For example, if an app used a service to compressed its storage, that would usually be a
background service.
• The other example it to listen the incoming SMS text at the background without notifying
the user.
Bound
• A service is bound when an application component binds to it by calling
bindService().
• A bound service offers a client-server interface that allows components to interact with
the service, send requests, receive results, and even do so across processes with
interprocess communication (IPC).
• A bound service runs only as long as another application component is bound to it.
• Multiple components can bind to the service at once, but when all of them unbind, the
service is destroyed.
Cont.…
• It's simply a matter of whether you implement a couple of callbackmethods:
onStartCommand() to allow components to start it and onBind() to allow
binding.
• Regardless of whether your service is started, bound, or both, any application component
can use the service (even from a separate application) in the same way that any
component can use an activity by starting it with an Intent.
• However, you can declare the service as private in the manifest file and block access from
other applications.
• This is discussed more in the section about Declaring the service in the manifest.
Difference between Service and IntentService
• So, we have revised the concept of Service and IntentService. in this section, we will look
upon some of the differences between the Service and IntentService, so that it will be
easier for you to find which one to use in which condition. Let’s see the difference, point
wise:
1. Usage: If you want some background task to be performed for a very long period of
time, then you should use the IntentService. But at the same time, you should take care
that there is no or very less communication with the main thread.
Cont.…
✓ If the communication is required then you can use main thread handler or broadcast
intents.
✓ You can use Service for the tasks that don’t require any UI and also it is not a very long
running task.
2. How to start?: To start a Service you need to call the onStartService() method while in
order to start a start IntentService you have to use Intent i.e. start the IntentService by
calling Context.startService(Intent).
3.Running Thread: Service always runs on the Main thread while the IntentService runs on
a separate Worker thread that is triggered from the Main thread.
Cont.…
4. Triggering Thread: Service can be triggered from any thread while the IntentService
can be triggered only from the Main thread i.e. firstly, the Intent is received on the Main
thread and after that, the Worker thread will be executed.
5. Main Thread blocking: If you are using Service then there are chances that your Main
thread will be blocked because Service runs on the Main thread.
Here, the tasks are performed in the form of Queue i.e. on the First Come First Serve
basis.
Cont.…
6. Stop Service: If you are using Service, then you have to stop the Service after using it
otherwise the Service will be there for an infinite period of time i.e. until your phone is in
normal state. So, to stop a Service you have to use stopService() or stopSelf().
But in the case of IntentService, there is no need of stopping the Service because the
Service will be automatically stopped once the work is done.
7. Interaction with the UI: If you are using IntentService, then you will find it difficult to
interact with the UI of the application. If you want to out some result of the IntentService in
your UI, then you have to take help of some Activity.
Cont.…
Characteristics of Mobile Applications
• Mobile is a rapidly growing industry that attracts companies from many industries.
• Despite being little software units with limited functionality, apps manage to provide
high-quality services and experiences to users.
• App Development Services can be a little expensive for you without making any
difference.
Chapter Summery
2/27/2024 MAD 26