Service in android

Activity is a foreground process. It contains user interaction. To run long running operations, we have to create a service.It will run in background. It does not contain user interaction.

Types of services:

1. Unbound Service: it runs in the background indefinitely even started activity with service ends also.
2. Bound Service : it will run till life time of activity.

 Activity can start service via startService()and it will stop via stopService().
If activity want to interact with service,it can use bindService().

first onCreate() is called, after onStartCommand is called with the intent data provided by the activity.


Android provides many predefined services exposed via Manager class.


Own services must be declared in the manifest file..


Some methods:

An activity can start a service via startService() method.

stop the service by using stopService() method.

bindService() - this is for activity to interact with service.

it needs "ServiceConnection" object.


Once service is started    onCreate()  is called.
onStart()



onBind()- the system calls when other component want to bind the service.

onStop() - for stopping the service.

OnDestroy()- called when service is no longer used. and it is destroyed.



Own services must be declared in manifest file.

how to declare own service:

<service android:name="your class"></service>
implementing class must extend the class   " service or one of its subclasses.


Ex: AsyncTask:
using services in android allows to have app running inthe background. if u want to do a simple task run in the background, use AsyncTask.

AsyncTask class:


private class someclassname extends AsyncTask<x,y,z>

protected void onPreExecute()
{
// starting new thread i.e    initilizing


protected void doInBackground()

// perform the action or do the task


protected void onPostExecute(){
// closing the process
}


 Example:

MainActivity.java
package com.example.servicetest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService(new Intent(this,ServiceAct.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


ServiceAct.java
package com.example.servicetest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ServiceAct extends Service{
    String tag="service tag";
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.d(tag, "service destroyed");   
    }
   
@Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Log.d(tag, "service started");
        stopSelf();
    }}

manifest permission

<service android:name="ServiceAct" />

Comments

Popular posts from this blog

What is Android ?