Thursday, 1 December 2011

Android interview questions - Services

What is Service in android?
Ans: A Service is a android application component which will be used to run long running task.Service will run in back ground. A Service can be created by extending "Service" class.
Unlike thread a Service will run in same process.Service can be started from other android components.

What are the different types of Services?
Ans: Android provide two types of Services:
  1. Local Services
  2. Remote Services
What are the Important/Basic methods in Services?
  • onCreate()
  • onBind()  -- Required if it is remote service
  • onStartCommand()  
  • onDestroy()
What is the significance of Sticky Intents in Services?
Services are low priority components in android and android system can kill services whenever it requires to allocate resources to higher priority tasks then by depending on Stick Intent value it will decide how to restart service.

Can I make my Service run all the time?
Ans: Theoretically it is supposed to be run all the time but generally Android will kill service whenever it need resources such as memory and processor , by returning START_STICKY intent from onStartCommand() we can ask Android system to restart it whenever resources available , another procedure is use AlarmManager to start service periodically.

What is foreground Service?
Ans:Unless background service it will have more priority , by using setForeGround() method we can make it foreground service.

Can I display or update UI from Service?
Ans: NO. To draw any UI component it should be done from UI thread. We can use Notification Manager to send notifications to the user.

If I start same service multiple times does it create multiple instances of Service?
Ans: NO.






3 comments: