Android Service Example Code

What is Android Service, Write an Example Code.

Android Service is a part that is utilized to perform operations on the foundation, for example, playing music, handle system exchanges, associating substance suppliers and so forth. It doesn't has any UI (client interface).

The Service runs out of sight uncertainly regardless of the fact that application is obliterated.

In addition, service can be limited by a part to perform intuitiveness and bury process correspondence (IPC).

The android.app.Service is subclass of ContextWrapper class.

Life Cycle of Android Service

There can be two manifestations of a service.The lifecycle of administration can take after two separate ways: began or bound.

  1. Started
  2. Bound

1) Started Service

A service is started when component (like activity) calls startService() method, now it runs in the background indefinitely. It is stopped by stopService() method. The service can stop itself by calling the stopSelf() method.

2) Bound Service

A service is bound when another component (e.g. client) calls bindService() method. The client can unbind the service by calling the unbindService() method.
The service cannot be stopped until all clients unbind the service.

Android Service Example

We should see the illustration of administration in android that plays a sound out of sight. Sound won't be halted regardless of the possibility that you change to another movement. To stop the sound, you have to stop the administration.

activity_main.xml

File: activity_main.xml
<RelativeLayout xmlns:androclass="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:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  
  
    <Button  
        android:id="@+id/buttonStart"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="19dp"  
        android:text="Start Service" />  
  
    <Button  
        android:id="@+id/buttonStop"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_above="@+id/buttonNext"  
        android:layout_alignRight="@+id/buttonStart"  
        android:layout_marginBottom="35dp"  
        android:text="Stop Service" />  
  
    <Button  
        android:id="@+id/buttonNext"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/buttonStop"  
        android:layout_centerVertical="true"  
        android:text="Next Page" />  
  
</RelativeLayout>  


activity_next.xml

File: activity_next.xml
<RelativeLayout xmlns:androclass="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:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="96dp"  
        android:layout_marginTop="112dp"  
        android:text="Next Page" />  
  
</RelativeLayout>  


Service class

File: MyService.java
  1. package com.example.serviceexampleaudio;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.media.MediaPlayer;  
  6. import android.os.IBinder;  
  7. import android.widget.Toast;  
  8. public class MyService extends Service {  
  9.  MediaPlayer myPlayer;  
  10.  @Override  
  11.  public IBinder onBind(Intent intent) {  
  12.   return null;  
  13.  }  
  14.  @Override  
  15.  public void onCreate() {  
  16.   Toast.makeText(this"Service Created", Toast.LENGTH_LONG).show();  
  17.    
  18.   myPlayer = MediaPlayer.create(this, R.raw.sun);  
  19.   myPlayer.setLooping(false); // Set looping  
  20.  }  
  21.  @Override  
  22.  public void onStart(Intent intent, int startid) {  
  23.   Toast.makeText(this"Service Started", Toast.LENGTH_LONG).show();  
  24.   myPlayer.start();  
  25.  }  
  26.  @Override  
  27.  public void onDestroy() {  
  28.   Toast.makeText(this"Service Stopped", Toast.LENGTH_LONG).show();  
  29.   myPlayer.stop();  
  30.  }  
  31. }  

Activity class

File: MainActivity.java
  1. package com.example.serviceexampleaudio;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. public class MainActivity extends Activity implements OnClickListener {  
  9.      Button buttonStart, buttonStop,buttonNext;  
  10.      @Override  
  11.      public void onCreate(Bundle savedInstanceState) {  
  12.       super.onCreate(savedInstanceState);  
  13.       setContentView(R.layout.activity_main);  
  14.   
  15.       buttonStart = (Button) findViewById(R.id.buttonStart);  
  16.       buttonStop = (Button) findViewById(R.id.buttonStop);  
  17.       buttonNext = (Button) findViewById(R.id.buttonNext);  
  18.   
  19.       buttonStart.setOnClickListener(this);  
  20.       buttonStop.setOnClickListener(this);  
  21.       buttonNext.setOnClickListener(this);  
  22.      }  
  23.      public void onClick(View src) {  
  24.       switch (src.getId()) {  
  25.       case R.id.buttonStart:  
  26.        startService(new Intent(this, MyService.class));  
  27.        break;  
  28.       case R.id.buttonStop:  
  29.        stopService(new Intent(this, MyService.class));  
  30.        break;  
  31.       case R.id.buttonNext:  
  32.        Intent intent=new Intent(this,NextPage.class);  
  33.        startActivity(intent);  
  34.        break;  
  35.       }  
  36.      }  
  37. }  

NextPage class

File: NextPage.java
package com.example.serviceexampleaudio;  
import android.app.Activity;  
import android.os.Bundle;  
  
public class NextPage extends Activity {  
 @Override  
 public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.activity_next);  
 }  
}  


Declare the Service in the AndroidManifest.xml file

File: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:androclass="http://schemas.android.com/apk/res/android"  
    package="com.example.serviceexampleaudio"  
    android:versionCode="1"  
    android:versionName="1.0" >  
  
    <uses-sdk  
        android:minSdkVersion="8"  
        android:targetSdkVersion="17" />  
  
    <application  
        android:allowBackup="true"  
        android:icon="@drawable/ic_launcher"  
        android:label="@string/app_name"  
        android:theme="@style/AppTheme" >  
        <activity  
            android:name="com.example.serviceexampleaudio.MainActivity"  
            android:label="@string/app_name" >  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
          
        <service  
            android:name=".MyService"  
            android:enabled="true" />  
         <activity  
            android:name=".NextPage"/>  
    </application>  
  
</manifest>  

Output:











0 comments:

Post a Comment

My Instagram