How to start Activity from Service in Android

In this android programming source code example, we are going to start Activity from Service 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 start Activity from Service 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.MapAndServiceActivity16">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <Button
            android:id="@+id/btnStart"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Start Service"
            android:layout_marginTop="5dp"
            android:background="@color/colorAccent"
            android:textColor="#ffffff"/>

        <Button
            android:id="@+id/btnStop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Stop Service"
            android:layout_marginTop="5dp"
            android:background="@color/colorAccent"
            android:textColor="#ffffff"/>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

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


public class MapAndServiceActivity16 extends AppCompatActivity {
    private Button btnStart;
    private Button btnStop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_and_service16);
        btnStart = (Button) findViewById(R.id.btnStart);
        btnStop = (Button) findViewById(R.id.btnStop);

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startService(new Intent(MapAndServiceActivity16.this, Service16.class));
                Toast.makeText(MapAndServiceActivity16.this, "Service Started", Toast.LENGTH_LONG).show();
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(new Intent(MapAndServiceActivity16.this, Service16.class));
                Toast.makeText(MapAndServiceActivity16.this, "Service Stoped", Toast.LENGTH_LONG).show();
            }
        });
    }
}

Service16.java

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

public class Service16 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();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //if device is not running use
        //Intent intent1 = new Intent(this, MapAndServiceActivity16.class);
       // intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       // startActivity(intent1);

        Intent intent1 = new Intent(this,YourActivity16.class);
        startActivity(intent1);

        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.Service16" />

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