Sie sind auf Seite 1von 15

Software for Mobile Devices

Android App Components


Lecture # 15

Online Group Piazza: https://piazza.com/fast_lahore/fall2018/cs440/home


Access Code: cs440
Android App Components

1. Intents
2. Alarm Managers & Job Schedulers
3. Content Providers
4. Notification Manager (Local Notifications & Push
Notifications)
5. Activity vs Fragments
6. Service
7. Broadcast Receiver
8. Contexts
Services

A Service is an Android application component without a UI that runs on the


main thread (of the hosting process). If you want the service code to run in a
Background Thread, then you must manage that yourself.

Used for:
1. Potentially long running tasks
2. Processing Big Data
3. Updating content providers
4. Backing up data.
5. Internet downloads

Types of Services:
1. Unbounded/ Started Service (Runs on main thread)
1. IntentService (Pre-Oreo) (Runs on background thread)
2. JobIntentService (Oreo+) (Runs on background thread)
2. Bounded Service (Runs on main thread)

Note: Service runs in the same process as the main thread of the application.

So does it run on a separate thread on background? NO!


Services

Started Service Rules to Start:


• Targeting Oreo +
1. Must have persistent notification attached to your service.
2. Start services with ‘startForegroundService’ from any android component.

Note: You have 5 seconds to move this started service to foreground and attach
persistent notification, otherwise you will get ANR (Application not responding) error.

How to start a service?

1. Create a class and extend it with service.


2. Implement overrideable methods i.e.(onStartCommand, onBind, onCreate,
onDestroy())
3. From your activity or other Android component call this service:
1. In oreo: startForegroundService(this, yourService.class);
2. In less than oreo startService(this, yourService.class)
4. Register that service in Android Manifest.

Note: A service is only started once, no matter how often you call the
startService() method. You need to stop ‘Started service’, otherwise it will keep on
running.
Handling
latest target
Version

In its onStartCommand() method call, the service


returns an int which defines its restart behavior in
case the service gets terminated by the Android
platform.

https://github.com/shahwaiz90/Android-
Services/blob/master/app/src/main/java/io/smd/androidservices/S
tartedService.java

Android Manifest Permission


Pre-Oreo Case!

http://www.vogella.com/tutorials/AndroidServices/ar
ticle.html
Intent Service

1. Apps which are not targeting Oreo or Oreo+ can use IntentService as well.

2. Only different between IntentService and Started Service is:

1. Intent Service works on Background/ Separate Thread.


2. Extends IntentService
3. After completion of its task it automatically stops itself, doesn’t need to
explicitly stop it.

3. Apps which are targeting Oreo or Oreo+ they must use JobIntentService.

Note: IntentService is a service, and is therefore subject to the new restrictions on background services. As a result, many apps that rely
on IntentService do not work properly when targeting Android 8.0 or higher. For this reason, Android Support Library 26.0.0 introduces a
new JobIntentService class, which provides the same functionality as IntentService but uses jobs instead of services when running on
Android 8.0 or higher.

https://developer.android.com/about/versions/oreo
/background
Job Intent Service

MainActivity

Android Manifest Permission

Separate class to implement


JobIntentService
Services
Bounded Services

1. Bound Services allow a connection to be established between the Android


component binding to the Service.

2. This connection is an IBinder which allows methods to be called on the


Service. (Java object (a Binder subclass) is exposed to the client which can
be used to access public methods on the Service)

3. Means: Activity or client can access/fetch/get information which is being


cooked/process/stored in the service (in other processes as well (IPC –
Inter-process communication).

4. Bounded service will get terminated by Android OS when the last client has
unbound.

5. That service which gives information to its clients (activity or another


service) is known as bounded service.
Categories of Bounded Service

Local Binding Remote Binding or Inter Process


Communication

Local Binding:
Implementation by Ibinder
Remote Binding:
Implementation by Messeger and AIDL
(Multi Threading Environment – Could
get complicated).

Google recommends to use Messenger


for remote binding.
BoundedService Part 1
BoundedService Part 2
Main Activity

Getting value from Service while residing in Activity!!


Two way Communication opened.
Intro to Broadcast Listeners
A broadcast receiver (receiver) is an Android component which allows you to
register for system or application events.

All registered receivers for an event are notified by the Android runtime once
this event happens.

System Broadcasts:
Broadcast Listeners?!
Steps to register for Broadcast listener
1. Declare your listener (class name) in manifest.

2. If your listener needs to access some resources which require permission,


then mention it as well in manifest.

3. Now create a separate class (Your broadcast Listener name which extends
from BroadcastReceiver)

To be continued in
next lecture!!!!
Tutorial Links

YouTube Playlist (services):


https://www.youtube.com/watch?v=7nxOTIBZ7Mw&list=PLfuE3hO
AeWhbm-_mNEbVdQuaac7Rd4TgZ

Services Code on Git:


https://github.com/shahwaiz90/Android-Services

Home Exercise:
Try to run above git code and do experiments on it. It has
following 3 services integrated.
StartService, Bounded Service and JobIntentService

Das könnte Ihnen auch gefallen