1

How to implement binder services, when client applications are in different processes?

  • use Binder
  • use aidl
  • use Messenger
  • either b or c

Answer: D either b or c
Description: Binder is used if both client and server are in same application.
If client and service (server) are in different applications, then this can be implemented in two ways:
1. Messenger - is a simple way to implement binder service, which is single threaded mode.
With this, your service can handle only one client request at a time.
2. aidl (Android interface definition language) - use it if you want multi threaded capability for your server/service
With this, your service can handle multiple requests at a time. But little complicated.
Make sure that you are writing always thread safe program in your service when using with aidls.
This is not recommended for most of the implementations.

2

What is the difference between service and intentservice in android?

  • i. Intentservice by default will create one separate thread to handle the service functionality. All the startservice requests for intentservice will be routed to that thread.
  • ii. Where as service by default runs in main thread. All the startservice requests will be routed to main thread by default.
  • iii. While implementing a service, programmer has to implement onCreate(), onStartCommand(), and onDestroy() methods.
  • iv. Where as while implementing IntentService programmer has to implement only onHandleIntent().
  • v. After starting IntentService, it will be automatically closed if there are no pending startService requests.
  • vi. Where as for normal service programmer has to stop the service explicitly either by using stopSelf() or stopService() methods.
  • vii. Don't try to touch UI from IntentService's onHandleIntent() method directly, as that function runs in a separate thread. (Not in main thread).
  • i & ii
  • I,ii,iii, & iv
  • I,ii,iii,iv,v, & vi
  • all are true

Answer: D all are true
Description:

3

What is not true about a binder service in android?

  • It is a client server model, where binder service will act as a server which responds to client requests with some data.
  • A bounded service can also be a started service.
  • onBind() function will be called every time a client binds to the binder service.
  • A serviceconnectionobject has to be passed to bindservice to monitor if connection is established between client and server.

Answer: C onBind() function will be called every time a client binds to the binder service.
Description: Binder Service's onBind() function will be called only once for the first client. From there on wards for every client call to bind this service, android will cache that binder object and return it with out calling onBind() function.

4

What is the difference between bindeservice and contentprovider? To share a functionality from one application to other application, which one to use?

  • startedService
  • bindedservice
  • both are useful
  • content provider

Answer: B bindedservice
Description: it is not a good practice to use started service if service wants to share some functionality with other components. binded services are far more faster and efficient than started service to share functionality with other components. it uses binders concept internally.
Note: Content providers are used to share data between 2 applications, not for sharing functionality.

5

What is the life cycle of a started service?

  • oncreate -> onstart() -> onBind() -> onUnbind() -> onDestroy()
  • oncreate -> onstart() -> onDestroy()
  • oncreate -> onstartCommand() -> onDestroy()
  • oncreate -> onstartCommand() -> onBind() -> onUnbind() -> onDestroy()

Answer: C oncreate -> onstartCommand() -> onDestroy()
Description: onCreate() -> onStartCommand() -> onDestroy(). This is the life cycle of a started service. onBind() and onUnbind() will come into picture for only binded services. previously in old versions onStart() was there in place of onStartCommand().

6

How to achieve security for services programmatically, in such a way that your service should not get triggered from outside applications?

  • Don't give any intent-filters for your service tag [or] put exported="false" in service tag [or] LocalServiceManager
  • Don't give any intent-filters for your service tag [or] LocalServiceManager
  • Don't give any intent-filters for your service tag [or] put imported="false" in service tag [or] LocalServiceManager
  • put exported="false" in service tag [or] LocalServiceManager

Answer: A Don't give any intent-filters for your service tag [or] put exported="false" in service tag [or] LocalServiceManager
Description: If you don't want to expose your service to outside apps, 3 ways are there. 1. Don't give intent-fiiter tag, so that outsiders can't specify intent action to trigger your service. or 2. User exported="false" in service tag, so that outside world cant trigger it. or 3. User local service manager.

7

To do some back ground functionality in an activity, which is better ? thread or service?

  • thread is better as long as thread is closely related with your UI and as long as programmer make sure that cleaning and creation of thread is done properly.
  • it is better to use services with thread, because threads in activity will have less priority compared to thread in a service in case if that activity is in background or stopped state. more over in case of low memory if it kills thread in activity, there is no way that android will recreate it. all these disadvantages are overcome in services with android.
  • you can either use option 1 or option 2, based on your requirement. But most of the times it is better to use option 2
  • Both thread and service are back ground components, so you can use either of them.

Answer: C you can either use option 1 or option 2, based on your requirement. But most of the times it is better to use option 2.
Description: it is better to use services with thread, because threads in activity will have less priority compared to thread in a service in case if that activity is in background or stopped state. more over in case of low memory if it kills thread in activity, there is no way that android will recreate it. all these disadvantages are overcome in services with android. But still if you want to use a thread with activity then as long as programmer make sure that cleaning and creation of thread is done properly, then there won't be any problem.

8

can I start a service from worker thread?

  • You can start service from any where, but still oncreate, onstartcommand runs in main thread only.
  • You can start service from any where, but after that oncreate, onstartcommand runs in that worker thread. so you have to make sure that you shouldn't touch UI from those functions.
  • No, it is illegal to do so. it will throw run time exception and program will be crashed.
  • it gives compile time error if we try to do so.

Answer: A You can start service from any where, but still oncreate, onstartcommand runs in main thread only.
Description: You can start service from any where, but still oncreate, onstartcommand runs in main thread only.

9

Update UI or toast from IntentService : What will happen if you try to touch UI or try to print a toast message from onHandleIntent() function of IntentService class ?

  • it works perfectly fine. we can touch UI from IntentService class.
  • you can't touch UI from it, or toast message will not be printed.
  • it behaves weirdly, either toast message may not appear or it appears and will be never removed. this happens because other thread is touch ui with out informing to main thread.If you try to touch other UI components, it will crash.
  • none of the above.

Answer: C it behaves weirdly, either toast message may not appear or it appears and will be never removed. this happens because other thread is touch ui with out informing to main thread.If you try to touch other UI components, it will crash.
Description: it behaves weirdly, either toast message may not appear or it appears and will be never removed. this happens because other threads can't touch UI with out informing to main thread.If you try to touch other UI components, it will crash.
IntentService onHandleIntent() functions runs in worker thread (not main thread). So don't try to touch UI elements from that function directly.

10

How to move a service to foreground?

  • A service always runs in background, so there nothing like foreground service.
  • call startForeground(int id, Notification notification);
  • call startService(Intent intent)
  • none

Answer: B call startForeground(int id, Notification notification);
Description:

11

What is not true about a binder service?

  • There may be situations where both client & server will be in the same process.
  • In case if client is in different process and service(Server) is in different process, then we should always use aidl to implement it.
  • Option 2 can also be implemented with Messenger class in case if we want to write single threaded service.
  • Binders internally uses Parcels.

Answer: B In case if client is in different process and service(Server) is in different process, then we should always use aidl to implement it.
Description: If client and server are in different processes, then there are two ways to implement service. 1.creating a singled threaded server using Binder and Messenger or 2. Creating multi threaded server using aidls.

12

What is not true about a Binder?

  • i. Binder is an IPC driver written on top of Linux IPC.
  • ii. Binder heavily uses Parcels in place of serializables for marshaling data to byte streams.
  • iii. Binders internally uses shared memory concept to make IPC faster.
  • iv. Binders heavily uses serializables in place of parcels for marshaling data to byte streams.
  • i
  • ii
  • iii
  • iv

Answer: D iv
Description: Binders won't use serialization concept for marshaling or converting data into byte streams. it uses Parcels instead.

13

If I want to create a binder service which supports multi threaded architecture, then which of the below concepts i have to use?

  • Use only Binder class.
  • Use Binder class with Messenger.
  • Use .aidls and implement the stub.
  • we can use either option2 or option3.

Answer: C Use .aidls and implement the stub.
Description: If we want to create a service with multi threads and supports binders then we have to use .aidls and implement the stub.

14

What is true about a binder service?

  • bindservice() function is an asynchronous call. it returns only after connection is established between client and server.
  • onBind() function will be called every time a client calls bindservice function.
  • onServiceDisconnected() function of ServiceConnection object will be called once a client says unbindeservice().
  • bindService() function is an asynchronous call which returns immediately. after returning, connection will be established after some time.

Answer: D bindService() function is an asynchronous call which returns immediately. after returning, connection will be established after some time.
Description: bindService() function is an asynchronous call which returns immediately. after returning, connection will be established after some time.

15

When implementing a bounded service, where client & server are in same process, which of the below functionalities I can do in inner Binder class?

  • Binder extended class can contain public methods, which can be accessed by client.
  • Binder extended class can return the object of outer service class, whose public functionalities can be accessed by client.
  • Binder extended class can return any other class's object of Service, whose public functionalities can be accessed by client.
  • select 1 if only option1 is true, 2 if both 1&2 are correct, 3 if all 3 are correct, else select 4.

Answer: C Binder extended class can return any other class's object of Service, whose public functionalities can be accessed by client.
Description: Using binder services we can share below functionalities from binder service 1. Public methods of Binder extended class 2. Public methods of Service class, by returning this pointer of the Service class 3. Any other Service inner class's public functionalities by returning object to that class.

16

What are the functions of a Binder class in android?

  • i. Binder is responsible for doing Marshaling & Un-Marshaling of the data that has to be transferred.
  • ii. Binder will do thread management in case of aidl, where it will create and dispatch a new thread when ever a new incoming request comes to the service.
  • iii. Binder class stub functionality has to be implemented by the programmer which contains interface functionalities for client.
  • i
  • i & ii
  • i, ii & iii
  • none

Answer: C i, ii & iii
Description: Below are the functionalities of Binder class.
1.Marshaling & Un-marshaling of out gong and incoming data across the processes
2. thread management for service class as incoming call from multiple clients may come from different threads.
3. Binder's stub functionality has to be implemented by the programmer.