How to check if a service is running in Android

In this android programming source code example, we are going to check if a service is running in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to check if a service is running in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MapAndService.MapAndServiceActivity13">

    <Button
        android:id="@+id/btnSer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text = "Check Service"
        android:background="@color/colorAccent"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.bluapp.androidview2.R;

public class MapAndServiceActivity13 extends AppCompatActivity {
    private Button btnService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_and_service13);
        btnService = (Button) findViewById(R.id.btnSer);
        btnService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMyServiceRunning(Service13.class)) {
                    stopService(new Intent(MapAndServiceActivity13.this, Service13.class));
                    Toast.makeText(MapAndServiceActivity13.this, "Service Stoped", Toast.LENGTH_LONG).show();
                } else {
                    startService(new Intent(MapAndServiceActivity13.this, Service13.class));
                    Toast.makeText(MapAndServiceActivity13.this, "Service Started", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}

Service13.java

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

public class Service13 extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed by user.", Toast.LENGTH_LONG).show();
    }
}

AndroidManifest.xml

<service android:name=".MapAndService.Service13" />

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment