Android Service
Anjan Debnath
Foreground Service
• it always run in foreground.
• this can avoid service object being recycled by android
system when android os do not have enough resources.
• Android foreground service can be interacted by user
through notification.
Started Service
• Started services are those that are launched by other
application components like an Activity or a Broadcast
Receiver.
• They can run indefinitely in the background until stopped
or destroyed by the system to free up resources.
Bound Service
• Started services cannot return results/values or interact
with its starting component.
• Bound services on the other hand can send data to the
launching component (client).
IPC
• IPC means Inter Process communication where two
applications or processes will communicate with each
other by passing some data between them.
• On Android, one process cannot normally access
the memory of another process.
• In both cases, the system must provide a "channel" that
allows two processes to communicate.
• IPC with Messenger and Handler
• IPC with AIDL (Android Interface Definition
Language)
Messenger
• messages to be passed across process boundaries
between client (client component) and server (service).
AIDL
• AIDL allows you to define the programming interface that
both the client and service agree upon in order to
communicate with each other using interprocess
communication (IPC).
Compared to AIDL
• When you need to perform IPC, using a Messenger for
your interface is simpler than using AIDL,
because Messengerqueues all calls to the service.
• A pure AIDL interface sends simultaneous requests to
the service, which must then handle multi-threading.
• For most applications, the service doesn't need to
perform multi-threading, so using a Messengerallows the
service to handle one call at a time.
• If it's important that your service be multi-threaded,
use AIDL to define your interface.
When Use AIDL
• Using AIDL is necessary only if you allow clients from
different applications to access your service for IPC and
want to handle multithreading in your service.
• If you do not need to perform concurrent IPC across
different applications, you should create your interface
by implementing a Binder or,
• if you want to perform IPC, but do not need to handle
multithreading, implement your interface using a
Messenger.
Server Client App with
AIDL
Inter Process
Communication (IPC)
• AIDL service provides IPC.
• When any task completed service will send response to
activities that actually bound that service
Remote Procedural Call
RPC
• AIDL service also provides RPC.
• When Activity request for data, Service will response
back instantly.
Project Structure
IHotPotato is the main AIDL interface
HotPotatoService is the Private Remote Service
IPeer.aidl
• When PEER_CHANGED event found new MeshID then
we convert that to MeshPeer.
• MeshProvider will sent this MeshPeer as a callback to
HotPotato Service via IPeer.aidl
• Activity that wants to be notified when new Peer
discovered , will register this callback via
getPeerInfo(IPeer peer)
• When Service receive the callback it will send
response to bounded activity.
Idata.aidl
• When DATA_RECEIVED event found new Data then we
convert that to MeshData.
• MeshProvider will sent this MeshData as a callback to
HotPotato Service via IData.aidl
• Activity that wants to be notified when new Data received
, will register this callback via getData(IData data)
• When Service receive the callback it will send
response to bounded activity.
RPC methods
• From Activity if we want peers List then activity will make
a RPC call to AIDL service via getPeersLists().
• According to request Service will response back with
peer list.
• From activity if we want to send message then again
activity will make a RPC call with
sendMessage(MeshData data)
• If we want self peer from activity then make a RPC call
with getSelfPeer();
Q&A
Thanks

Aidl service

  • 1.
  • 2.
    Foreground Service • italways run in foreground. • this can avoid service object being recycled by android system when android os do not have enough resources. • Android foreground service can be interacted by user through notification.
  • 3.
    Started Service • Startedservices are those that are launched by other application components like an Activity or a Broadcast Receiver. • They can run indefinitely in the background until stopped or destroyed by the system to free up resources.
  • 4.
    Bound Service • Startedservices cannot return results/values or interact with its starting component. • Bound services on the other hand can send data to the launching component (client).
  • 5.
    IPC • IPC meansInter Process communication where two applications or processes will communicate with each other by passing some data between them. • On Android, one process cannot normally access the memory of another process. • In both cases, the system must provide a "channel" that allows two processes to communicate. • IPC with Messenger and Handler • IPC with AIDL (Android Interface Definition Language)
  • 6.
    Messenger • messages tobe passed across process boundaries between client (client component) and server (service).
  • 7.
    AIDL • AIDL allowsyou to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).
  • 8.
    Compared to AIDL •When you need to perform IPC, using a Messenger for your interface is simpler than using AIDL, because Messengerqueues all calls to the service. • A pure AIDL interface sends simultaneous requests to the service, which must then handle multi-threading. • For most applications, the service doesn't need to perform multi-threading, so using a Messengerallows the service to handle one call at a time. • If it's important that your service be multi-threaded, use AIDL to define your interface.
  • 9.
    When Use AIDL •Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. • If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, • if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger.
  • 10.
  • 11.
    Inter Process Communication (IPC) •AIDL service provides IPC. • When any task completed service will send response to activities that actually bound that service
  • 12.
    Remote Procedural Call RPC •AIDL service also provides RPC. • When Activity request for data, Service will response back instantly.
  • 13.
    Project Structure IHotPotato isthe main AIDL interface HotPotatoService is the Private Remote Service
  • 14.
    IPeer.aidl • When PEER_CHANGEDevent found new MeshID then we convert that to MeshPeer. • MeshProvider will sent this MeshPeer as a callback to HotPotato Service via IPeer.aidl • Activity that wants to be notified when new Peer discovered , will register this callback via getPeerInfo(IPeer peer) • When Service receive the callback it will send response to bounded activity.
  • 15.
    Idata.aidl • When DATA_RECEIVEDevent found new Data then we convert that to MeshData. • MeshProvider will sent this MeshData as a callback to HotPotato Service via IData.aidl • Activity that wants to be notified when new Data received , will register this callback via getData(IData data) • When Service receive the callback it will send response to bounded activity.
  • 16.
    RPC methods • FromActivity if we want peers List then activity will make a RPC call to AIDL service via getPeersLists(). • According to request Service will response back with peer list. • From activity if we want to send message then again activity will make a RPC call with sendMessage(MeshData data) • If we want self peer from activity then make a RPC call with getSelfPeer();
  • 17.
  • 18.