0% found this document useful (0 votes)
14 views26 pages

Chapter 3

Chapter Three discusses the concepts of Intents and Services in Android development, detailing how Intents facilitate communication between app components and the different types of Intents (explicit and implicit). It also explains the various types of Services, including foreground, background, and bound services, as well as the differences between Service and IntentService. Additionally, the chapter highlights the characteristics of mobile applications and concludes with a summary of key points related to communication between services and activities.

Uploaded by

jamsibro140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views26 pages

Chapter 3

Chapter Three discusses the concepts of Intents and Services in Android development, detailing how Intents facilitate communication between app components and the different types of Intents (explicit and implicit). It also explains the various types of Services, including foreground, background, and bound services, as well as the differences between Service and IntentService. Additionally, the chapter highlights the characteristics of mobile applications and concludes with a summary of key points related to communication between services and activities.

Uploaded by

jamsibro140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Chapter Three

Intents and Services


Outline

▪ 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).

• The Internet is the global system of interconnected computer networks.

• The Internet carries an unlimited range of information resources and services,


such as the interlinked hypertext documents and applications of the Web,
electronic mail, telephony, and file sharing.
Cont.…
• Internet is a group of computer systems connected from all around the
world. The Internet protocol suite is a framework defined through the
Internet standards.

• Methods are divided right into a layered set of protocols on this


architecture.
What Is An Intent?

• An intent is a messaging object used to request any action from another


app component.

• Intents facilitate communication between different components in


several ways.

• The intent is used to launch an activity, start the services, broadcast


receivers, display a web page, dial a phone call, send messages from one
activity to another activity, and so on.
Cont.…
• Android Intent is the message that is passed between components such
as activities, content providers, broadcast receivers, services etc.

• It is generally used with startActivity() method to invoke activity,


broadcast receivers etc.

• The dictionary meaning of intent is intention or purpose. So, it can be


described as the intention to do action.
Types of Intent
There are two types of intent.

I. Explicit Intents: connected internal activities of application, suppose if you


wants to connect one activity to another activity, we can do this quote by explicit
intent.

▪ This explicit Intent work internally within an application to perform navigation and
data transfer.

Intent intent_var = new Intent(getApplicationContext(), SecondActivity.class);


startActivity(intent_var);
Cont.…
II. Implicit Intents: in implicit intent it is not required to specify the component.
• It do not name a specific component, but instead declare a general action to
perform, which allows a component from another app to handle it.
• In android when create implicit intents, the android system will search for
matching components by comparing the contents of intent with intent filters
which defined in the manifest file of other apps on the device.
Cont..
• In this implicit Intents If the matching component found, the system starts that
component and sends it to the Intent object.

• 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 open the page ‘http://www.google.com’ through your android system


default browser
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(intent);
Cont.…
Example2: to open the page ‘http://www.google.com’ in webview component you
specify the webview and set the Uri to it.
Webview_var.getSettings().setJavaScriptEnabled(true);
String url = " http://www.google.com";
webview_var.loadUrl(url);
Intent methods
• startActivity() – The Intent object is passed to this method to launch a new
activity or get an existing activity to do something new/to navigate to other activity
in the same application.

• 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.

• sendBroadcast() – The Intent object is passed to this method to deliver the


message to all interested broadcast receivers.
public class ReceivSms extends BroadcastReceiver {

}
Services

• Service is a component that runs in the background to perform long-running


operations.
For example
1. A service might play music in the background while the user is in a different
application.
2. Fetch data over the network without blocking user interaction with an
activity.
Cont..

• A service is implemented as a subclass of Service class as follows


public class MyService extends Service {

}
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.

But, in case of IntentService, there is no involvement of 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.

• Because of the growing popularity of smartphones and tablets, mobile application


development is becoming more popular among business owners all over the world.

• A mobile application is a software program that runs on a mobile device, such as a


smartphone or tablet computer.

• 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

▪ How do you communicate between an Android service and an


activity?
▪ Give the specific example?
▪ Describe the different types of services available in Android?
▪ Discuss the role of IntentService in Android?
Cont.…

2/27/2024 MAD 26

You might also like