Thursday 27 October 2016

Services

A Service is an application part that can perform long-running operations out of sight and does not give a UI.
 Another application segment can begin an administration and it will keep on running out of sight regardless of the possibility that the client changes to another application.
Also, a segment can tie to an administration to cooperate with it and even perform interprocess correspondence (IPC).
For instance, an administration may handle arrange exchanges, play music, perform document I/O, or collaborate with a substance supplier, all from the foundation.
It is subclass of ContextWrapperclass.

Example :-  Music player and Alarm.

Services of lifecycle,There are  types of service.

1) Start Service :-

 We can begin an administration from a movement or other application part by passing an Intent (determining the administration to begin) to startService().
The Android framework calls the administration's onStartCommand() technique and passes it the Intent. (You ought to never call onStartCommand() straightforwardly.)

     Syntax :-
                    Using Intent Class
                    Intent in=new Intent(Currentclass.this,TargetClass);
                    startService(in);

2) Stop Service :-

A began benefit must deal with its own particular lifecycle. That is, the framework does not stop or decimate the administration unless it must recuperate framework memory and the administration keeps on pursuing onStartCommand() returns.
 In this way, the administration must stop itself by calling stopSelf() or another part can stop it by calling stopService().

     Syntax :-
                     Using Intent Class
                     Intent in=new Intent(Currentclass.this,TargetClass);
                     stopService(in);

3) Bound Service :-

A bound administration is one that permits application parts to tie to it by calling bindService() keeping in mind the end goal to make a long-standing association (and by and large does not permit segments to begin it by calling startService()).
You ought to make a bound administration when you need to interface with the administration from exercises and different parts in your application or to uncover some of your application's usefulness to different applications, through interprocess correspondence (IPC).

Code For Service in Android :-

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
 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" 
 android:orientation="vertical" 
 tools:context="com.kiranapp.MainActivity">
 
<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Start Service" 
 android:id="@+id/button" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" />
 
<Button  
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  
 android:text="Stop Service"  
 android:id="@+id/button"  
 android:layout_marginTop="20dp"  
 android:layout_gravity="center_horizontal" /> 
 
</LinearLayout>

MainActvity.java :-

package com.kiranapp;

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

public class MainActivity extends AppCompatActivity
 {
    Button startserbtn,stopserbtn;

@Override
    protected void onCreate(Bundle savedInstanceState)
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startserbtn=(Button)findViewById(R.id.button_start);
    stopserbtn=(Button)findViewById(R.id.button_stop);
    startserbtn.setOnClickListener(new View.OnClickListener()
   {

@Override
    public void onClick(View v)
   {
    Intent in=new Intent(MainActivity.this,MyServices.class);
    startService(in);
    }
    });
   stopserbtn.setOnClickListener(new View.OnClickListener()
  {

@Override
    public void onClick(View v)
   {
    Intent in=new Intent(MainActivity.this,MyServices.class);
    stopService(in);
    }
    });
    }
}

Output :-

                     Run the app on emulator or on mobile phone  

No comments:

Post a Comment