Mobile Applications Lecture 7 - Foreground and Background Services
Mobile Applications Lecture 7 - Foreground and Background Services
Lecture 7 – Foreground
and Background
Services
Foreground Services
Background Tasks
Intents and Intent Filters
Started Service Simple Example
Startbtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startService(new Intent(MainActivity.this,
MusicService.class));
}});
Stopbtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
stopService(new Intent(MainActivity.this,
MusicService.class));
}});
Adding service to Manifest file
<service android:name=“.MusicService”/>
Foreground Services
Starting in Android 13 (API level 33), users can dismiss the notification
associated with a foreground service by default. To do so, users perform a
swipe gesture on the notification.
Traditionally, the notification isn't dismissed unless the foreground service is
either stopped or removed from the foreground.
If you want the notification non-dismissable by the user, pass true into the
setOngoing() method when you create your notification using
Notification.Builder.
Notifications
Apps that target Android 9 (API level 28) or higher and use foreground services need
to request the FOREGROUND_SERVICE permission, as shown in the following code
snippet.
This is a normal permission, so the system automatically grants it to the requesting
app.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application ...>
...
</application>
</manifest>
Starting a Foreground Service
Before you request the system to run a service as a foreground service, start
the service itself.
Inside the service, usually in onStartCommand(), you can request that your
service run in the foreground. To do so, call startForeground().
This method takes two parameters: a positive integer that uniquely identifies
the notification in the status bar and the Notification object itself.
Starting a Foreground Service
Notification notification =
new Notification.Builder(this, CHANNEL_DEFAULT_IMPORTANCE)
.setContentTitle(getText(R.string.notification_title))
.setContentText(getText(R.string.notification_message))
.setSmallIcon(R.drawable.icon)
.setContentIntent(intent)
.setTicker(getText(R.string.ticker_text))
.build();
// Notification ID cannot be 0.
startForeground(ONGOING_NOTIFICATION_ID, notification);
Background Service
A Background Service is a service that runs only when the app is running so
it’ll get terminated when the app is terminated.
To create a Background Service
(1) create a new Class and have it extend the Service class.
(2) Inside the class, override the onBind() and onStartCommand() methods.
(3) Add service to manifest file.
(4) Start the service.
Background Service
Background Service
Bound Service
If your service is private to your own application and runs in the same
process as the client, which is common, create your interface by extending
the Binder class and returning an instance of it from onBind().
The client receives the Binder and can use it to directly access public
methods available in either the Binder implementation or the Service.
This is the preferred technique when your service is merely a background
worker for your own application.
The only use case when this is not the preferred way to create your
interface is if your service is used by other applications or across separate
processes.
Extend Binder Class
With Android 5.0 (API level 21) and later, you can start a service with
JobScheduler.
For versions earlier than Android 5.0 (API level 21), you can start a service by
using methods of the Service class.
You can start a service to perform a one-time operation (such as
downloading a file) by passing an Intent to startService(). The Intent
describes the service to start and carries any necessary data.
If the service is designed with a client-server interface, you can bind to the
service from another component by passing an Intent to bindService().
Delivering a Broadcast
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.
You'll typically use an explicit intent to start a component in your own app,
because you know the class name of the activity or service you want to start.
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.
Implicit Intents
do not name a specific component, but instead declare a general action to
perform, which allows a component from another app to handle it.
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 Types