---
title: "How to start and stop services  and broadcast in Android"  
description: ".In this article, I am going to show you how to start services and broadcast them through Intent class. Here, there is one single interface and three"  
author: "Nitish Kesarwani"  
published: 2017-12-19  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12732/how-to-start-and-stop-services-and-broadcast-in-android  
category: "android apps"  
tags: ["broadcast", "service", "class", "interface", "object"]  
reading_time: 3 minutes  

---

# How to start and stop services  and broadcast in Android

##### Introduction of services and broadcast in Android

In this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370), I am going to show you how to start [services](https://yourviews.mindstick.com/view/82090/advantages-of-using-fuel-delivery-services-at-construction-sites) and [broadcast](https://www.mindstick.com/articles/1633/broadcastreceiver-in-android) them through Intent class. Here, there is one single [interface](https://www.mindstick.com/articles/12036/interface-in-c-sharp) and three buttons named startService stopService and broadcast. These two things are started through intent class and its object is used to startActivity along with. I am using Toast class which is used for [giving](https://yourviews.mindstick.com/view/81621/natural-disasters-in-2020-giving-us-alarm-warning) the flash [message](https://www.mindstick.com/interview/682/which-are-the-various-message-map-macros) on the [screen](https://www.mindstick.com/blog/53499/top-5-big-screen-tvs-for-the-best-tv-shows-of-2019) that the usual user has seen during sending e-mail.Toast is called by makeText() [method](https://www.mindstick.com/forum/166/webservice-method) in which three values are passed separated by comma(,) [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) and another is that using [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) such as onStartCommand and onDestroy in useServices.java file

###### AndroidManifest.xml

```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapsample.msclient009.activity_method">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />         </intent-filter>
    </activity>
    <service android:name=".ServiceUse" />
    <receiver android:name=".IntentExample">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
        </intent-filter>
    </receiver>
</application>
</manifest>
```

###### Activity_Main.xml

```
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mapsample.msclient009.activity_method.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="229dp"
        android:layout_height="47dp"
        android:text="StartService"
        tools:layout_editor_absoluteX="80dp"
        tools:layout_editor_absoluteY="10dp"
        android:onClick="startService"
         />

    <Button
        android:id="@+id/button2"
        android:layout_width="252dp"
        android:layout_height="44dp"
        android:text="StopService()"
        tools:layout_editor_absoluteX="80dp"
        tools:layout_editor_absoluteY="100dp"
        android:onClick="stopService"
         />
    <Button
        android:id="@+id/btn3"
        android:layout_width="252dp"
        android:layout_height="44dp"
        android:text="BroadcastIntent"
        tools:layout_editor_absoluteX="80dp"
        tools:layout_editor_absoluteY="150dp"
        android:onClick="broadcastIntent"
   /> </LinearLayout>
</android.support.constraint.ConstraintLayout>
```

###### ServiceUse.java

```
package com.mapsample.msclient009.activity_method;

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

/**
 * Created by msclient009 on 12-12-2017.
 */

public class ServiceUse extends Service {

    @Nullable
    @Override
    public android.os.IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public int  onStartCommand(Intent intent,int flags,int startId)
    {
        Toast.makeText(this, "service Started", Toast.LENGTH_SHORT).show();
          return START_STICKY;
    }
    public  void onDestroy()
    { super.onDestroy();
        Toast.makeText(this, "service Destroy", Toast.LENGTH_SHORT).show();
    }
}
```

```
<resources>
    <string name="app_name">Activity_Method</string>
    <string name="start">Start Service</string>
    <string name="stop">Stop Service</string>
    <string name="broad">Broadcast</string>
</resources>
```

![How to start and stop services  and broadcast in Android](https://www.mindstick.com/mindstickarticle/51ff65fd-aa9e-4ee4-b10c-506dbc99dda1/images/b1bd7079-8085-48da-b8df-fda5e2f2eac4.png)

\

\

\

---

Original Source: https://www.mindstick.com/articles/12732/how-to-start-and-stop-services-and-broadcast-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
