Wednesday, August 31, 2016

Service and It's Life cycle

Hi .... Dear All ....  Today I am uploading a superb example of Service with it's life cycle.

Service
@ It's an Application component  which has 
1. Activity which has No layout
2. Running in Background
3.  Used for long running task
4. Service has it’s life cycle

Drawback 
@ It's running with main thread so we should use asynctask with service

Type

1. Started      2. Bound 

@ To create a service, you must create a subclass of Service.
@ In your implementation, you need to override some callback methods that handle key aspects of the      service lifecycle and provide a mechanism for components to bind to the service.
@ Callback methods are onCreate(),onStartCommand(),onBind(),onDestroy().
See the below example -  Register your service in manifest file 
like   <service android:name=".MyServ"></service>

A. Output will be like  


B. XML file     activity_main.xml is as 
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.exam.ravi.servicedemo.MainActivity">

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="StartService"

        android:id="@+id/button1"

        android:layout_alignParentTop="true"

        android:layout_alignParentStart="true"

        android:layout_marginTop="44dp"

        android:onClick="startServ"

        android:layout_toStartOf="@+id/button2"     />

    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="StopService"

        android:id="@+id/button2"

        android:onClick="stopServ"

        android:layout_alignTop="@+id/button1"

        android:layout_alignParentEnd="true" />
  
</RelativeLayout>

C. Java Code is as 
   C - 1     Service class is as   MyServ.java
   package com.exam.ravi.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

public class MyServ extends Service
{
    @Nullable    @Override    public IBinder onBind(Intent intent)   
                                  // Used for Bind service Local or Remote    {
        return null;
    }


    @Override    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this,"onstartcommand called",Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override    public void onCreate() {
        super.onCreate();
        Toast.makeText(this,"onCreate called",Toast.LENGTH_LONG).show();
    }

    @Override    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this,"onDestroy called",Toast.LENGTH_LONG).show();
    }
}

C - 2  MainActivity.java is as 

package com.exam.ravi.servicedemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    Intent iob;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iob=new Intent(this,MyServ.class);
    }
    public void startServ(View v)

    {
    startService(iob);
    }
    public void stopServ(View v)
    {
       stopService(iob);
    }
}



No comments:

Post a Comment