Pressing a button is supposed to start service and display text, but nothing happens when I click the button

I’m new to Java and I was trying to make a simple program to display text when a button is pressed by using services. For some reason nothing happens when I press the “Start Service” and “Stop Service” buttons. Here’s my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void startService(View view) {
    startService(new Intent(getBaseContext(), MyService.class));
}
public void stopService(View view) {
    stopService(new Intent(getBaseContext(), MyService.class));
}

public static class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }
}

And the buttons-

<Button
    android:id="@+id/button"
    android:layout_width="132dp"
    android:layout_height="105dp"
    android:layout_marginTop="8dp"
    android:onClick="startService"
    android:text="@string/Buttton1"
    android:visibility="visible"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button2"
    android:layout_width="128dp"
    android:layout_height="108dp"
    android:layout_marginTop="8dp"
    android:onClick="stopService"
    android:text="@string/Button2"
    android:visibility="visible"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button" />

A few things wrong.
1: I don’t think your service should be static.
2: Did you declare your service in your AndroidManifes.xml?
3: You don’t need to use getBaseContext()

  • If you’re in an activity use this
  • If you’re in a fragment use getActivity()

So you have to declare all your services same as all your activities in the android manifest.To do that navigate to your apps manifest located under app>manifest>AndroidManifext.xml.Next, you need to add a service tag under application with the name of your service.
Example code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.android.myapplication">

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <service android:name=".MyService"/> <-----This is were you declared your service
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Everything should work fine after that.

I was using a fragment that’s why it wasn’t working. I used MainActivity and it worked. Thanks for the answers!