this is an old conversation between me and you : To achieve this, you can use the Activity Recognition API provided by Google Play services. Here’s how you can create a new interface to recognize user activities and display the confidence values:

Step 1: Add Google Play services to your project.

Add the following to your app’s build.gradle file, under the dependencies section:

implementation ‘com.google.android.gms:play-services-location:18.0.0’


Step 2: Create a new class for detecting activities.

Create a new class called ActivityRecognitionService and extend it from IntentService:

import android.app.IntentService;
import android.content.Intent;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;

public class ActivityRecognitionService extends IntentService {
    
    public ActivityRecognitionService() {
        super(“ActivityRecognitionService”);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (ActivityRecognitionResult.hasResult(intent)) {
            ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
            DetectedActivity mostProbableActivity = result.getMostProbableActivity();
            int confidence = mostProbableActivity.getConfidence();
            int activityType = mostProbableActivity.getType();
            
            // TODO: Handle the detected activity here
        }
    }
}


Step 3: Update your AndroidManifest.xml.

Add the following service inside the &lt;application&gt; tag of your AndroidManifest.xml:

<service
    android:name=“.ActivityRecognitionService”
    android:exported=“false” />


Step 4: Request activity recognition updates in your MainActivity.

In your MainActivity class, add the following code to request activity recognition updates:

private final int REQUEST_CODE_ACTIVITY_RECOGNITION = 100;
private PendingIntent activityRecognitionPendingIntent;
private ActivityRecognitionClient activityRecognitionClient;

private void requestActivityUpdates() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, REQUEST_CODE_ACTIVITY_RECOGNITION);
        return;
    }
    
    activityRecognitionClient = new ActivityRecognitionClient(this);

    Intent intent = new Intent(this, ActivityRecognitionService.class);
    activityRecognitionPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    activityRecognitionClient.requestActivityUpdates(1000, activityRecognitionPendingIntent);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CODE_ACTIVITY_RECOGNITION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        requestActivityUpdates();
    }
    // Handle other permission results here
}


Now, when you call requestActivityUpdates() method, it will request for activity recognition updates. Handle the detected activities in the onHandleIntent() method of your ActivityRecognitionService class.

Step 5: Create a new Activity to display confidence values.

Create a new activity called ActivityConfidenceActivity with a layout that contains a TextView for each activity (walking, running, sitting, and jumping). Update the TextView with the confidence values in the onHandleIntent() method of the ActivityRecognitionService.

Don’t forget to add your new activity to your AndroidManifest.xml. Now, when the user opens this activity, they will see the confidence values for each detected activity.
j’ai une error la activityRecognitionClient = new ActivityRecognitionClient(this); dit que ‘ActivityRecognitionClient’ is abstract; cannot be instantiated
I apologize for the confusion. You should use ActivityRecognition.getClient(this) instead of instantiating ActivityRecognitionClient. Here’s how to update the requestActivityUpdates() method:

Change this line:

activityRecognitionClient = new ActivityRecognitionClient(this);


to:

activityRecognitionClient = ActivityRecognition.getClient(this);


So, the updated requestActivityUpdates() method should look like this:

private void requestActivityUpdates() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, REQUEST_CODE_ACTIVITY_RECOGNITION);
        return;
    }

    activityRecognitionClient = ActivityRecognition.getClient(this);

    Intent intent = new Intent(this, ActivityRecognitionService.class);
    activityRecognitionPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    activityRecognitionClient.requestActivityUpdates(1000, activityRecognitionPendingIntent);
}


Now, you should no longer encounter the error.
so know i added an activity ActivityConfidenceActivity contient textView for (walking, running, sitting, and jumping how can you provide the code that should out in onHandleIntent
In order to update the TextViews in ActivityConfidenceActivity from ActivityRecognitionService, you can use a BroadcastReceiver. Here’s how you can do it:

Step 1: Create a BroadcastReceiver in your ActivityConfidenceActivity.

public class ActivityConfidenceActivity extends AppCompatActivity {
    private BroadcastReceiver activityReceiver;

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

        activityReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                int type = intent.getIntExtra(“type”, -1);
                int confidence = intent.getIntExtra(“confidence”, 0);

                // Update the TextViews according to the activity type and confidence
            }
        };
    }
}


Step 2: Register and unregister the BroadcastReceiver.

@Override
protected void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(activityReceiver, new IntentFilter(“activity_update”));
}

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(activityReceiver);
}


Step 3: Update the onHandleIntent() method in ActivityRecognitionService.

@Override
protected void onHandleIntent(Intent intent) {
    if (ActivityRecognitionResult.hasResult(intent)) {
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
        DetectedActivity mostProbableActivity = result.getMostProbableActivity();
        int confidence = mostProbableActivity.getConfidence();
        int activityType = mostProbableActivity.getType();

        // Send a broadcast with the activity type and confidence
        Intent broadcastIntent = new Intent(“activity_update”);
        broadcastIntent.putExtra(“type”, activityType);
        broadcastIntent.putExtra(“confidence”, confidence);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
    }
}


Now, your ActivityConfidenceActivity will receive updates from the ActivityRecognitionService. Update the TextViews according to the received activityType and confidence values in the onReceive() method of the BroadcastReceiver.

Please make sure you have added the required import statements for BroadcastReceiver, LocalBroadcastManager, and other classes used in the code snippets.
can you give me // Update the TextViews according to the activity type and confidence code
Certainly! First, you need to add the TextViews for walking, running, sitting, and jumping in your activity_confidence.xml layout file. Add the following to your layout:

<TextView
    android:id=“@+id/tv_walking”
    android:layout_width=“wrap_content”
    android:layout_height=“wrap_content”
    android:text=“Walking: " />

<TextView
    android:id=”@+id/tv_running"
    android:layout_width=“wrap_content”
    android:layout_height=“wrap_content”
    android:text=“Running: " />

<TextView
    android:id=”@+id/tv_sitting"
    android:layout_width=“wrap_content”
    android:layout_height=“wrap_content”
    android:text=“Sitting: " />

<TextView
    android:id=”@+id/tv_jumping"
    android:layout_width=“wrap_content”
    android:layout_height=“wrap_content”
    android:text=“Jumping: " />


Now, you can reference these TextViews in your ActivityConfidenceActivity. Update the onReceive() method in the BroadcastReceiver as follows:

activityReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int type = intent.getIntExtra(“type”, -1);
        int confidence = intent.getIntExtra(“confidence”, 0);

        TextView tvWalking = findViewById(R.id.tv_walking);
        TextView tvRunning = findViewById(R.id.tv_running);
        TextView tvSitting = findViewById(R.id.tv_sitting);
        TextView tvJumping = findViewById(R.id.tv_jumping);

        String confidenceText = " (” + confidence + “%)”;

        switch (type) {
            case DetectedActivity.WALKING:
                tvWalking.setText(“Walking:” + confidenceText);
                break;
            case DetectedActivity.RUNNING:
                tvRunning.setText(“Running:” + confidenceText);
                break;
            case DetectedActivity.STILL:
                tvSitting.setText(“Sitting:” + confidenceText);
                break;
            case DetectedActivity.ON_FOOT: // You can use this as an approximation for jumping
                tvJumping.setText(“Jumping:” + confidenceText);
                break;
            default:
                // Handle other activity types if needed
                break;
        }
    }
};


This code will update the corresponding TextView based on the detected activity type and display the confidence value in the format “Activity: (Confidence%)”. Adjust the case values and other code parts if necessary according to your specific activity types and layout structure.
==> this is my main package org.project.ayoub_akanoun_projet;

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

import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

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

import org.project.ayoub_akanoun_projet.activitiesRecognition.ActivityConfidenceActivity;
import org.project.ayoub_akanoun_projet.activitiesRecognition.ActivityRecognitionService;

    public class Main2Activity extends AppCompatActivity {

        private final int REQUEST_CODE_ACTIVITY_RECOGNITION = 100;
        private PendingIntent activityRecognitionPendingIntent;
        private ActivityRecognitionClient activityRecognitionClient;
        private Button button;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                requestActivityUpdates();
            }

            button = findViewById(R.id.button);
            button.setOnClickListener(v -> {
                Intent intent = new Intent(Main2Activity.this, ActivityConfidenceActivity.class);
                startActivity(intent);
            });
        }
        @RequiresApi(api = Build.VERSION_CODES.Q)
        private void requestActivityUpdates() {
            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACTIVITY_RECOGNITION}, REQUEST_CODE_ACTIVITY_RECOGNITION);
                Toast.makeText(this, "permession diened", Toast.LENGTH_SHORT).show();
                return;
            }

            activityRecognitionClient = ActivityRecognition.getClient(this);

            Intent intent = new Intent(this, ActivityRecognitionService.class);
            activityRecognitionPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            activityRecognitionClient.requestActivityUpdates(1000, activityRecognitionPendingIntent);
        }
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            if (requestCode == REQUEST_CODE_ACTIVITY_RECOGNITION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    requestActivityUpdates();
                }
            }
        }



}

when the ActivityConfidence showed it didn't updates