i want to build an app android java to recogniaze user activity using  ActivityRecognitionClient it work on the android studion emulator device but it does not work on phisical phones both are having android 11 this is my code : 
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.widget.Toast;

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

import com.google.android.gms.location.ActivityRecognition;
import com.google.android.gms.location.ActivityRecognitionClient;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;

public class BackgroundDetectedActivity extends Service {

    private PendingIntent pendingIntent;
    private ActivityRecognitionClient activityRecognitionClient;

    IBinder binder = new LocalBinder();

    public class LocalBinder extends Binder {

        public BackgroundDetectedActivity getServerInstance() {
            return BackgroundDetectedActivity.this;
        }
    }

    public BackgroundDetectedActivity() {

    }

    @SuppressLint("UnspecifiedImmutableFlag")
    @Override
    @RequiresApi(api = Build.VERSION_CODES.Q)
    public void onCreate() {
        super.onCreate();
        activityRecognitionClient = ActivityRecognition.getClient(this);
        Intent intentService = new Intent(this, ActivityRecognitionService.class);
       // pendingIntent = PendingIntent.getService(this, 1, intentService, PendingIntent.FLAG_UPDATE_CURRENT);
        pendingIntent = PendingIntent.getService(this, 1, intentService, PendingIntent.FLAG_UPDATE_CURRENT);
        requestActivityUpdatesButtonHandler();
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    public void requestActivityUpdatesButtonHandler() {
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
       // Task<Void> task = activityRecognitionClient.requestActivityUpdates(1000, pendingIntent);
        Task<Void> task = activityRecognitionClient.requestActivityUpdates(500, pendingIntent);
        task.addOnSuccessListener(new OnSuccessListener<Void>() {

            @Override
            public void onSuccess(Void result) {
                Toast.makeText(getApplicationContext(), "Mises à jour d'activité demandées avec succès", Toast.LENGTH_SHORT).show();
            }
        });

        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), "La demande de mises à jour d'activité n'a pas pu démarrer", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        removeActivityUpdate();
    }

    public void removeActivityUpdate() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        Task<Void> task = activityRecognitionClient.removeActivityUpdates(pendingIntent);

        task.addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void result) {
                Toast.makeText(getApplicationContext(), "Mises à jour d'activité supprimées avec succès !", Toast.LENGTH_SHORT).show();
            }
        });

        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), "Échec de la suppression des mises à jour d'activité!", Toast.LENGTH_SHORT).show();
            }
        });
    }


}

import static android.content.ContentValues.TAG;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;

import java.util.ArrayList;

public class ActivityRecognitionService extends IntentService {

    public ActivityRecognitionService() {
        super("ActivityRecognitionService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

        // Get the list of the probable activities associated with the current state of the
        // device. Each activity is associated with a confidence level, which is an int between
        // 0 and 100.
        ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();

        for (DetectedActivity activity : detectedActivities) {
            Log.e(TAG, "Detection d'activité: " + activity.getType() + ", " + activity.getConfidence());
            broadcastActivity(activity);
        }
    }

    private void broadcastActivity(DetectedActivity activity) {
        Intent intent = new Intent("activity_intent");
        intent.putExtra("type", activity.getType());
        intent.putExtra("confidence", activity.getConfidence());
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
}

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

import android.Manifest;
import android.app.NotificationManager;
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.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.location.ActivityRecognition;
import com.google.android.gms.location.ActivityRecognitionClient;
import com.google.android.gms.location.DetectedActivity;

import org.project.ayoub_akanoun_projet.R;
import org.project.ayoub_akanoun_projet.databinding.ActivityConfidenceBinding;
import org.project.ayoub_akanoun_projet.db.DatabaseHelper;
import org.project.ayoub_akanoun_projet.entities.Activity;

import java.util.Date;

@RequiresApi(api = Build.VERSION_CODES.Q)
public class ActivityConfidenceActivity extends AppCompatActivity {
    private Button btnStart, btnStop;
    private TextView txt_activity, txt_confidence;
    private ImageView img_activity;
    DatabaseHelper db;
    String email;


    private BroadcastReceiver activityReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confidence);
        txt_activity = findViewById(R.id.txt_activity);
        txt_confidence = findViewById(R.id.txt_confidence);
        img_activity = findViewById(R.id.img_activity);
        btnStart = findViewById(R.id.btn_start);
        btnStop = findViewById(R.id.btn_stop);
        db = new DatabaseHelper(this);
        if (ActivityCompat.checkSelfPermission(ActivityConfidenceActivity.this,
                Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_GRANTED) {
            // When permission grated
            // call Method
            btnStart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    txt_activity.setText("starting the service may take a while  ...");
                    txt_confidence.setText("");
                    img_activity.setImageResource(0);
                    requestActivityUpdates();

                }

            });

        } else {
            // When permission denied
            // Request permission
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.ACTIVITY_RECOGNITION
            }, 44);
        }


        activityReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d("ActivityUpdate", "Received update");
                int type = intent.getIntExtra("type", -1);
                int confidence = intent.getIntExtra("confidence", 0);
                // Update the TextViews according to the activity type and confidence
                handleUserActivity(type, confidence);


            }
        };

        btnStop.setOnClickListener((v) -> {
                    Intent intent1 = new Intent(ActivityConfidenceActivity.this, BackgroundDetectedActivity.class);
                    stopService(intent1);
                    txt_activity.setText("Service stopped");
                    txt_confidence.setText("To start the service click on start button");
                    img_activity.setImageResource(0);
                    if (!TextUtils.isEmpty(db.getLastActivity()[4])){
                        db.updateEndDate(new Date().toString(),Long.parseLong(db.getLastActivity()[0]));
                    }

                }
        );
    }


    private void handleUserActivity(int type, int confidence) {
        String label = "unknown";
        int icon = R.drawable.activity_unknown;
        switch (type) {
            case DetectedActivity.IN_VEHICLE: {
                icon = R.drawable.activity_driving;
                label = "in_vehicle";
                break;
            }
            case DetectedActivity.ON_BICYCLE: {
                icon = R.drawable.activity_on_bicycle;
                label = "on_bicycle";
                break;
            }
            case DetectedActivity.ON_FOOT: {
                icon = R.drawable.activity_jumpping;
                label = "on_foot";
                break;
            }

            case DetectedActivity.RUNNING: {
                icon = R.drawable.activity_running;
                label = "running";
                break;
            }
            case DetectedActivity.STILL: {
                icon = R.drawable.activity_still;
                label = "still";
                break;
            }
            case DetectedActivity.WALKING: {
                icon = R.drawable.activity_walking;
                label = "walking";
                break;
            }
            case DetectedActivity.UNKNOWN: {
                icon = R.drawable.activity_unknown;
                label = "unknown";
                break;
            }
        }
        Log.d("ActivityUpdate", "User activity: " + label + ", Confidence: " + confidence);

        if (confidence > 50) {
            txt_activity.setText(label);
            txt_confidence.setText("" + confidence);
            img_activity.setImageResource(icon);

            if(!TextUtils.isEmpty(label)){
              String []data=  db.getLastActivity();
              if(data[4]=="" && !data[2].equals(label)){
                  db.updateEndDate(new Date().toString(),Long.parseLong(data[0]));
                  db.addActivity(new Activity(email,label,new Date().toString(),""));
              }else{
                  db.addActivity(new Activity(email,label,new Date().toString(),""));
              }
            }

            // notification
            NotificationCompat.Builder builder = new NotificationCompat.Builder(ActivityConfidenceActivity.this, NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID)
                    .setSmallIcon(icon)
                    .setContentTitle("Activity changed")
                    .setContentText("Activity changed to " + label + " with confidence " + confidence + "%")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(ActivityConfidenceActivity.this);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "permession den", Toast.LENGTH_SHORT).show();
                return;
            }
            notificationManager.notify(0, builder.build());
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(this).registerReceiver(activityReceiver, new IntentFilter("activity_intent"));
    }
    @Override
    protected void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(activityReceiver);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 44) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
              return;
            }
        }
    }

    private void requestActivityUpdates() {
        Intent intent1 = new Intent(ActivityConfidenceActivity.this, BackgroundDetectedActivity.class);
        startService(intent1);
    }


}