1 Create the Service Class
First, create MyService.java. This class handles the background work. Because a service runs on the main UI thread by default, heavy operations should be offloaded to a separate Thread here.
package com.example.android_service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Service Created");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service Started");
Toast.makeText(this, "Service is running...", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Service Destroyed");
Toast.makeText(this, "Service stopped.", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return null; // Not a bound service
}
}
2 Register in Manifest
Android components must be declared. Add the <service> tag inside your AndroidManifest.xml.
<!-- Inside your app/src/main/AndroidManifest.xml -->
<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/Theme.Android_service">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Add your service here -->
<service android:name=".MyService" android:exported="false" />
</application>
3 Update the Layout
Add “Start” and “Stop” buttons to your activity_main.xml layout file.
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnStartService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service"
app:layout_constraintBottom_toTopOf="@+id/btnStopService"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="@+id/btnStopService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Stop Service"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnStartService" />
</androidx.constraintlayout.widget.ConstraintLayout>
4 Wire it up in MainActivity
Finally, trigger the service using Intents in your MainActivity.java file.
package com.example.android_service;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
Button btnStart = findViewById(R.id.btnStartService);
Button btnStop = findViewById(R.id.btnStopService);
btnStart.setOnClickListener(v -> {
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
});
btnStop.setOnClickListener(v -> {
Intent serviceIntent = new Intent(this, MyService.class);
stopService(serviceIntent);
});
}
}
Application Outcome
START SERVICE
STOP SERVICE
STOP SERVICE
Service running…