articles

Home / DeveloperSection / Articles / How to start and stop services and broadcast in Android

How to start and stop services and broadcast in Android

Nitish Kesarwani 3212 19-Dec-2017
Introduction of services and broadcast in Android

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 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 the flash message on the screen that the usual user has seen during sending e-mail.Toast is called by makeText() method in which three values are passed separated by comma(,) operator and another is that using methods 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    





Updated 07-Sep-2019

Leave Comment

Comments

Liked By