Site icon WebArtDeveloper

How to create, start and stop Service in Android

In this tutorial how to create, start and stop Service in Android is shown.

Android Service is a component that is used to perform operations on the background such as playing music,handle network transactions etc, which does not have any UI(User Interface).

The service is running in the background indefinitely even if application is destroyed.

A Service has two States:

Methods of Service are as follows:

  1. onStartCommand(): This method is called whenever another component such as an activity request get the service started.
  2. onBind(): It is called when another component wants to bind with service by calling bindService().
  3. onUnbind(): It is called when all clients have disconnected from a particular interface.
  4. onRebind(): It is called when new clients have connected to the service after it had previously been notified that all had disconnected in its onUnbind() method.
  5. onCreate(): It is called when Service is created for the first time.
  6. onDestroy(): It is called whenever Service is no longer used and is being destroyed.

The figure of Service Lifecycle is as follows:

Code:

strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<string name=”app_name”>Demo</string>
<string name=”action_settings”>Settings</string>
<string name=”h”>Program of Service</string>
<string name=”mes1″>Start Service</string>
<string name=”mes2″>Stop Service</string>

</resources>

 

activity_main.xml

<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”
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_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”82dp”
android:text=”@string/h” />

<Button
android:id=”@ id/button1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@ id/textView1″
android:layout_marginTop=”117dp”
android:layout_toLeftOf=”@ id/textView1″
android:onClick=”start”
android:text=”@string/mes1″ />

<Button
android:id=”@ id/button2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBottom=”@ id/button1″
android:layout_toRightOf=”@ id/textView1″
android:onClick=”stop”
android:text=”@string/mes2″ />

</RelativeLayout>

 

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(“Service”,”onCreate() invoked”);
}

//method to start service
public void start(View v)
{
startService(new Intent(getBaseContext(),Secondjava.class));
}

//method to stop service
public void stop(View v)
{
stopService(new Intent(getBaseContext(),Secondjava.class));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

 

Secondjava.java

package com.example.demo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class Secondjava extends Service{

@Override
public IBinder onBind(Intent arg0)
{
return null;
}

@Override
public int onStartCommand(Intent intent,int flags,int startID)
{
Toast.makeText(this, “Service Started”, Toast.LENGTH_LONG).show();
return START_STICKY;
}

@Override
public void onDestroy()
{
super.onDestroy();
Toast.makeText(this, “Service Stopped”, Toast.LENGTH_LONG).show();
}
}

 

AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.demo”
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.demo.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=”.Secondjava”></service>
</application>

</manifest>

https://youtu.be/fQWxf2NoeFs

https://youtu.be/D-9hsUhdiWc

Exit mobile version