I`ve got an application that should be modified and improved:

package com.google.sl;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.google.android.gms.location.ActivityRecognition;
import com.google.android.gms.location.ActivityTransition;
import com.google.android.gms.location.ActivityTransitionRequest;
import com.google.android.gms.location.DetectedActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final int ACTIVITY_RECOGNITION_REQUEST_CODE = 100;
    private TextView activityTextView;
    private Button stopButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        activityTextView = findViewById(R.id.activityTextView);
        stopButton = findViewById(R.id.stopButton);

        // Request permission if not granted
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, ACTIVITY_RECOGNITION_REQUEST_CODE);
        } else {
            // Permission already granted
            startActivityTransitionUpdates();
        }

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopActivityTransitionUpdates();
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == ACTIVITY_RECOGNITION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startActivityTransitionUpdates();
            } else {
                Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private PendingIntent transitionPendingIntent;
    private BroadcastReceiver transitionBroadcastReceiver;

    @SuppressLint("MissingPermission")
    private void startActivityTransitionUpdates() {
        // Build transitions you're interested in
        List<ActivityTransition> transitions = new ArrayList<>();

        int[] activityTypes = {DetectedActivity.WALKING, DetectedActivity.RUNNING, DetectedActivity.IN_VEHICLE, DetectedActivity.ON_BICYCLE, DetectedActivity.STILL, DetectedActivity.ON_FOOT, DetectedActivity.TILTING};

        for (int activityType : activityTypes) {
            transitions.add(new ActivityTransition.Builder().setActivityType(activityType).setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER).build());
            transitions.add(new ActivityTransition.Builder().setActivityType(activityType).setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT).build());
        }

        ActivityTransitionRequest request = new ActivityTransitionRequest(transitions);

        // Create a pending intent
        Intent intent = new Intent(this, TransitionReceiver.class);
        transitionPendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);

        // Register for activity transitions
        ActivityRecognition.getClient(this).requestActivityTransitionUpdates(request, transitionPendingIntent).addOnSuccessListener(aVoid -> {
            Toast.makeText(this, "Started activity updates", Toast.LENGTH_SHORT).show();
            stopButton.setVisibility(View.VISIBLE);
        }).addOnFailureListener(e -> {
            Toast.makeText(this, "Failed to start activity updates", Toast.LENGTH_SHORT).show();
        });

        // Register receiver to update UI
        transitionBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String activityInfo = intent.getStringExtra("activity");
                activityTextView.setText("Current Activity: " + activityInfo);
            }
        };
        LocalBroadcastManager.getInstance(this).registerReceiver(transitionBroadcastReceiver, new IntentFilter("TRANSITION_ACTION"));

        // Initialize database helper
        DatabaseHelper.initializeInstance(this);
    }

    @SuppressLint("MissingPermission")
    private void stopActivityTransitionUpdates() {
        ActivityRecognition.getClient(this).removeActivityTransitionUpdates(transitionPendingIntent).addOnSuccessListener(aVoid -> {
            Toast.makeText(this, "Stopped activity updates", Toast.LENGTH_SHORT).show();
            stopButton.setVisibility(View.GONE);
            activityTextView.setText("Activity monitoring stopped.");
        }).addOnFailureListener(e -> {
            Toast.makeText(this, "Failed to stop activity updates", Toast.LENGTH_SHORT).show();
        });

        if (transitionBroadcastReceiver != null) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(transitionBroadcastReceiver);
            transitionBroadcastReceiver = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopActivityTransitionUpdates();
    }
}


it doesnt recognize activity due to there isn`t background process according to android policy