1. What is the purposes of Service in Android?

Android Service is used to perform long running jobs off the UI thread. A typical long running tasks can be periodic downloading of data from internet, saving multiple records into database, perform file I/O, fetching your phone contacts list, etc. For such long running tasks, Service is used to avoid UI lags and makes user experience better.

2. Explain service lifecycle methods?

A service can be run by the system, If someone calls Context.startService() or bindService() method.

  1. onStartCommand() – This method is called when the service be started by calling startService(). Once this method executes, the service is started and can run in the background indefinitely. If you implement this, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService().  If you are defining your service as, bounded service then you don’t need to implement this method.
  2. onBind() – You need to override this method, only if you are defining your service as bounded service. This method is called, when another component wants to bind with the service by calling bindService(). In your implementation of this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder. You must always implement this method, but if you don’t want to allow binding, then you should return null.
  3. onCreate()– This method is called while the service is first created. Here all the service initialization is done. This method is never called again.
  4. onDestroy() – The system calls this method when the service is no longer used and is being destroyed. This method is used to, clean up any resources such as threads, registered listeners, receivers, etc. This is the last call the service receives.

3. What is the difference between bound and unbounded service?

  1. Bound Service – Service which call indefinitely in between activity. An Android component may bind itself to a Service using bindservice (). A bound service would run as long as the other application components are bound to it. As soon as they unbind, the service destroys itself.
  2. Unbound Service – Service which call at the life span of calling activity. In this case, an application component starts the service, and it would continue to run in the background, even if the original component that initiated it is destroyed. For instance, when started, a service would continue to play music in the background indefinitely.

4. What are the different clock types supported for AlarmService?

Android supports two clock types for alarm service. Once is elapsed real time and other is real time clock (RTC).  Elapsed real time uses the time since device last booted. Real time clock (RTC) uses UTC time for alarm service clock. RTC is most commonly used for setting alarm service in android.

5. How to define a service in manifest

All the services  used in the app need to be registered in application Manifest. Services are declared as shown below

<!--Service declared in manifest -->
<service android:name=".HelloService"
android:exported="false"/>

6. How to pass data to an IntentService ?

You can pass data as bundle to IntentService before you start.

Intent intent = new Intent(Intent.ACTION_SYNC, null, this, DownloadService.class);

/* Send optional extras to Download IntentService */
intent.putExtra("url", url);
intent.putExtra("receiver", mReceiver);
intent.putExtra("requestId", 101);

startService(intent);

7. What is an alarm service and explain it’s need with real-world example

Alarm service is used to run tasks periodically at given interval. You can design application like alrm, birthday reminder, or AlarmManager can be used to initiate long running operations such as syncing data from server once a day.   Once an Alarm Started, this will execute until it is stopped explicitly or until device reboots.

8. How to stop service?

Bounded service stops itself once it finishes its tasks. But for unbounded service we need to call stopSelf() or stopService() method.

9. Can an IntentService execute multiple tasks sequentially?

No. Intent Services are designed to perform one task at single point of time.  However tasks can be queued.

10. How can we make the AlarmService run forever even after device reboot

Once you start an AlarmService, it runs forever until your device restarts. Once your device restart, you have to start the service explicitly to run it forever again. You have to register BroadcastReceiver to handle boot event.

11. What are the key differences between a service and IntentService in Android?

Service IntentService
Service can be used in tasks with no UI, but shouldn’t be too long. If you need to perform long tasks, you must create a new thread with in Service IntentService can be used in long running tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents.
Service can be started using startService() method IntentService can be started using startService() method and it triggers onHandleIntent() method.
Service can be triggered from any thread IntentService must be triggered from Main Thread
Service runs in background but it runs on the Main Thread of the application. IntentService runs on a separate worker thread
The Service may block the Main Thread of the application. The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.
You must call stopSelf() or stopService() to stop a service once your its job is done. IntentService stops itself when it finishes its job so you never have to call stopSelf()

 

 

Stacktips

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages. Follow us on Twitter

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.
  • Yogesh Wani
  • Yogesh Wani

    Bus Demo

  • Yogesh Wani

    public class RouteDetails extends Activity {

    TextView mTxtDisplayFrom,mTxtDisplayTo,mTxtDisplayDuration,mTxtDisplayTime,mTxtDisplayFair,mTxtDisplayStop;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_route_details);

    mTxtDisplayFrom = (TextView) findViewById(R.id.txtDisplayFrom);

    mTxtDisplayTo = (TextView) findViewById(R.id.txtDisplayTo);

    mTxtDisplayDuration = (TextView) findViewById(R.id.txtDisplayDuration);

    mTxtDisplayTime = (TextView) findViewById(R.id.txtDisplayTime);

    mTxtDisplayFair = (TextView) findViewById(R.id.txtDisplayFair);

    mTxtDisplayStop = (TextView) findViewById(R.id.txtDisplayStops);

    Intent intent = getIntent();

    Routes routes = (Routes) intent.getSerializableExtra(“route”);

    mTxtDisplayFrom.setText(routes.getFrom());

    mTxtDisplayTo.setText(routes.getTo());

    mTxtDisplayDuration.setText(routes.getDuration());

    mTxtDisplayTime.setText(routes.getTime());

    mTxtDisplayFair.setText(routes.getFair());

    ArrayList stopsarray = new ArrayList();

    stopsarray = routes.getmStops();

    for(int i =0;i<stopsarray.size();i++){

    if(i==0){

    mTxtDisplayStop.setText(stopsarray.get(i));

    }else{

    mTxtDisplayStop.append(" ,"+stopsarray.get(i));

    }

    }

    }

    }

  • Yogesh Wani

    public class Routes implements Serializable {

    String mFrom,mTo,mDuration,mTime,mFair;

    ArrayList mStops;

    public Routes( String from, String to, String duration, String time, String fair, ArrayList stops) {

    this.mFrom = from;

    this.mTo = to;

    this.mDuration = duration;

    this.mTime = time;

    this.mFair = fair;

    this.mStops = stops;

    }

    public String getFrom() {

    return mFrom;

    }

    public void setFrom(String from) {

    this.mFrom = from;

    }

    public String getTo() {

    return mTo;

    }

    public void setTo(String to) {

    this.mTo = to;

    }

    public String getDuration() {

    return mDuration;

    }

    public void setDuration(String duration) {

    this.mDuration = duration;

    }

    public String getTime() {

    return mTime;

    }

    public void setTime(String time) {

    this.mTime = time;

    }

    public String getFair() {

    return mFair;

    }

    public void setFair(String fair) {

    this.mFair = fair;

    }

    public ArrayList getmStops() {

    return mStops;

    }

    public void setmStops(ArrayList mStops) {

    this.mStops = mStops;

    }

    }

  • Yogesh Wani

    public class RoutesAdapter extends BaseAdapter {

    Context mContext;

    ArrayList mRoutesList;

    RouteView mRouteView;

    public RoutesAdapter(Context context, ArrayList routesList) {

    this.mContext = context;

    this.mRoutesList = routesList;

    }

    @Override

    public int getCount() {

    return mRoutesList.size();

    }

    @Override

    public Object getItem(int position) {

    return null;

    }

    @Override

    public long getItemId(int position) {

    return 0;

    }

    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView==null){

    mRouteView = new RouteView(mContext);

    }else {

    mRouteView = (RouteView) convertView;

    }

    mRouteView.setRoutes(mRoutesList.get(position));

    return mRouteView;

    }

    }

  • Yogesh Wani

    public class RouteView extends LinearLayout {

    TextView mTxtFrom,mTxtTo,mTxtTime;

    Routes routes;

    public RouteView(Context context) {

    super(context);

    LayoutInflater inflater = LayoutInflater.from(context);

    View view = inflater.inflate(R.layout.lay_routeview,this);

    mTxtFrom = (TextView) view.findViewById(R.id.txtFrom);

    mTxtTo = (TextView) view.findViewById(R.id.txtTo);

    mTxtTime = (TextView) view.findViewById(R.id.txtTime);

    }

    public Routes getRoutes() {

    return routes;

    }

    public void setRoutes(Routes routes) {

    this.routes = routes;

    mTxtFrom.setText(routes.getFrom());

    mTxtTo.setText(routes.getTo());

    mTxtTime.setText(routes.getTime());

    }

    }

  • Yogesh Wani

    public class WebUtil {

    public static ArrayList getRouteDetails(){

    URL url;

    HttpURLConnection httpURLConnection;

    try {

    url = new URL(“http://bitcodetech.co/ws_ios_assignment/routes.php”);

    httpURLConnection = (HttpURLConnection) url.openConnection();

    httpURLConnection.connect();

    InputStream in = new BufferedInputStream(httpURLConnection.getInputStream());

    byte[] arr = new byte[1024];

    int count;

    StringBuilder stringBuilder = new StringBuilder();

    while ((count=(in.read(arr)))!=-1){

    stringBuilder.append(new String(arr,0,count));

    }

    JSONObject jsonObject = new JSONObject(stringBuilder.toString());

    JSONArray routes = jsonObject.getJSONArray(“routes”);

    ArrayList routesarray = new ArrayList();

    for(int i=0;i<routes.length();i++){

    JSONObject obj = routes.getJSONObject(i);

    String from = obj.getString("from");

    String to = obj.getString("to");

    String duration = obj.getString("duration");

    String time = obj.getString("time");

    String fair = obj.getString("fair");

    ArrayList stopsarray = new ArrayList();

    JSONArray stops = obj.getJSONArray(“stops”);

    for(int j=0;j<stops.length();j++){

    JSONObject stopobj = stops.getJSONObject(j);

    String name = stopobj.getString("name");

    stopsarray.add(name);

    }

    Routes routes1 = new Routes(from,to,duration,time,fair,stopsarray);

    routesarray.add(routes1);

    }

    return routesarray;

    } catch (MalformedURLException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    } catch (JSONException e) {

    e.printStackTrace();

    }

    return null;

    }

    }

  • Yogesh Wani

    public class ThreadWorker extends AsyncTask<Object,Object,ArrayList> {

    MainActivity.RouteHandler routeHandler;

    Context context;

    public ThreadWorker(Context context, MainActivity.RouteHandler routeHandler) {

    this.context = context;

    this.routeHandler = routeHandler;

    }

    @Override

    protected ArrayList doInBackground(Object… params) {

    ArrayList routes = WebUtil.getRouteDetails();

    return routes;

    }

    @Override

    protected void onPostExecute(ArrayList routes) {

    super.onPostExecute(routes);

    Message message = new Message();

    message.obj = routes;

    routeHandler.handleMessage(message);

    }

    }

  • Yogesh Wani

    public class MainActivity extends ListActivity {

    ListView mListView;

    ArrayList mRoutesList;

    RoutesAdapter mRoutesAdapter;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    init();

    new ThreadWorker(this,new RouteHandler()).execute((Object[]) null);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override

    public void onItemClick(AdapterView parent, View view, int position, long id) {

    Intent intent = new Intent(MainActivity.this,RouteDetails.class);

    intent.putExtra(“route”,mRoutesList.get(position));

    startActivity(intent);

    }

    });

    }

    private void init() {

    mListView = getListView();

    mRoutesList = new ArrayList();

    mRoutesAdapter = new RoutesAdapter(this,mRoutesList);

    mListView.setAdapter(mRoutesAdapter);

    }

    class RouteHandler extends Handler{

    @Override

    public void handleMessage(Message msg) {

    super.handleMessage(msg);

    ArrayList routes = (ArrayList) msg.obj;

    mRoutesList.addAll(routes);

    mRoutesAdapter.notifyDataSetChanged();

    }

    }

    }