i'm traying to implement google activty recognition api to  to detect my activity i have those classes the app works in the android studio emulator (it detect activity) but not workin on my phone : 
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.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) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            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);
    }
}

@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);
    }


}