0

I used Google ActivityRecognitionApi for tracking whether i am walking or not. But It seems too slow. What's the problem? It doesn't related to internet connectivity.

[Walking.java]

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_walking);
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(ActivityRecognition.API).addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();
    mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
    Intent intent = new Intent(this, ActivityRecognitionService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 1000, pendingIntent);
}
@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(Walking.this, "Hello1", Toast.LENGTH_SHORT).show();
    Log.e("Connection Failed : ", "Connection Failed");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Toast.makeText(Walking.this, "Hello2", Toast.LENGTH_SHORT).show();
    Log.e("Connection Failed : ", "Connection Failed");
}

[ActivityRecognitionService.java]

import java.util.List;
public class ActivityRecognitionService extends IntentService {
    public static final String ACTION_IntentService = "com.example.android.skywalker.RESPONSE";
    public static final String ACTIVITY_RESULT = "RESULT";
    public Intent localIntent;
    public ActivityRecognitionService() {
        super("ActivityRecognitionService");
    }
    public ActivityRecognitionService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if(ActivityRecognitionResult.hasResult(intent)) {
            ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
            handleDetectedActivities(result.getProbableActivities());
        }
    }

    private void handleDetectedActivities(List<DetectedActivity> probableActivities) {
        int Flag = 0;

        for(DetectedActivity activity : probableActivities) {
            if(activity.getType() == DetectedActivity.ON_FOOT) {
                Flag = 1;
                if (activity.getConfidence() >= 50) Log.e("YES", "YES");
                break;
            }
        }

        //Intent intent = new Intent(ActivityRecognitionService.this, Walking.class);
        if (Flag == 0) {
            //localIntent = new Intent(Walking.BROADCAST_ACTION).putExtra(Walking.RESPONSE_STATUS, "YES");
            Log.e("NO", "NO");
        }
    }
}
Sergey
  • 7,985
  • 4
  • 48
  • 80

1 Answers1

2

I don't think there is any problem with your code, it looks exactly the same as mine. The normal rate for detection is 3 seconds -> 6 seconds if your "detectionIntervalMillis" value = 0. But when you switch between 2 different activities (for example: from walking -> in the car....) it will take more, maybe 20s to 40s.

Hoa Nguyen
  • 91
  • 9
  • 1
    I change between still and walking (or vice versa) and it takes up to 1 minute! this is useless – user924 Nov 13 '21 at 13:39