In this code make the map in background showing the geofenced area in red and live location with a blue dot - package live.stazy.tenant;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;

import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingClient;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MyForegroundServiceLocation extends Service {
    private static final String CHANNEL_ID = "ForegroundServiceChannel";
    private static final String TAG = "MyForegroundService";
    private GeofencingClient geofencingClient;
    private Handler handler;
    private Runnable runnable;

    @Override
    public void onCreate() {
        super.onCreate();


        geofencingClient = LocationServices.getGeofencingClient(this);

        createNotificationChannel();
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                checkGeofenceStatus();
                handler.postDelayed(this, 5000); // Check every 10 seconds
            }
        };
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @SuppressLint("ForegroundServiceType")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText("Monitoring geofencing")
                .setSmallIcon(R.drawable.lockstazy)
                .build();

        startForeground(1, notification);

        handler.post(runnable);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            if (manager != null) {
                manager.createNotificationChannel(serviceChannel);
            }
        }
    }

    private void checkGeofenceStatus() {
        Geofence geofence = new Geofence.Builder()
                .setRequestId("GeofenceID")
                .setCircularRegion(28.6359552, 77.3095424, 10) // Example coordinates and radius
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build();

        GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()
                .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
                .addGeofence(geofence)
                .build();

        PendingIntent pendingIntent = getGeofencePendingIntent();

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        Task<Void> geofenceTask = geofencingClient.addGeofences(geofencingRequest, pendingIntent);
        geofenceTask.addOnSuccessListener(aVoid -> {
            Toast.makeText(getApplicationContext(), "Geofencing setup successful", Toast.LENGTH_SHORT).show();
        }).addOnFailureListener(e -> {
            Toast.makeText(getApplicationContext(), "Geofencing setup failed: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        });
    }

    private PendingIntent getGeofencePendingIntent() {
        Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
        return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    }




//    private void checkGeofenceStatus() {
//        Geofence geofence = new Geofence.Builder()
//                .setRequestId("GeofenceID")
//                .setCircularRegion(63.5888, 154.4931, 1000) // Example coordinates and radius
//                .setExpirationDuration(Geofence.NEVER_EXPIRE)
//                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
//                .build();
//
//        GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()
//                .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
//                .addGeofence(geofence)
//                .build();
//
//        PendingIntent pendingIntent = getGeofencePendingIntent();
//
//        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//        }
//        Task<Void> geofenceTask = geofencingClient.addGeofences(geofencingRequest, pendingIntent);
//        geofenceTask.addOnSuccessListener(aVoid -> {
//            // Geofencing setup successful, you can check location updates here
//            Toast.makeText(getApplicationContext(), "Geofencing setup successful", Toast.LENGTH_SHORT).show();
//            updateFirebaseDatabase(true);
//        }).addOnFailureListener(e -> {
//            Toast.makeText(getApplicationContext(), "Geofencing setup failed"+e.getMessage(), Toast.LENGTH_SHORT).show();
//            updateFirebaseDatabase(false); // For demonstration, assuming outside geofence
//        });
//    }
//
//
//    private PendingIntent getGeofencePendingIntent() {
//        Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
//        return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
//    }
//



//    private void updateFirebaseDatabase(boolean isInsideGeofence)
//
//    {
//        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
//        String userId = "neeraj";
//        String status = isInsideGeofence ? "present" : "absent";
//
//
//        databaseReference.child("user_atten").child(userId).child("status").addValueEventListener(new ValueEventListener() {
//            @Override
//            public void onDataChange(@NonNull DataSnapshot snapshot) {
//                if (snapshot.exists())
//                {
//                    String num = String.valueOf(Integer.parseInt(snapshot.getValue().toString())+1);
//                    FirebaseDatabase.getInstance().getReference().child("user_atten").child(userId).child("number").setValue(num);
//                    FirebaseDatabase.getInstance().getReference().child("user_atten").child(userId).child("value").setValue(status);
//                    Toast.makeText(getApplicationContext(),"dont worry bro i am running",Toast.LENGTH_SHORT).show();
//                }
//                else
//                {
//                    FirebaseDatabase.getInstance().getReference().child("user_atten").child(userId).child("number").setValue("1");
//                    FirebaseDatabase.getInstance().getReference().child("user_atten").child(userId).child("value").setValue(status);
//                }
//            }
//
//            @Override
//            public void onCancelled(@NonNull DatabaseError error) {
//                Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
//            }
//        });
//
//
//    }



}