0

Have been trying to use Google Activity Recognition API, but until now, can't get a single result.

I have this function in Main Activity

private void requestTransactionsUpdates() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) !=
                PackageManager.PERMISSION_GRANTED) {
            return;
        }

        List transitions = new ArrayList<>();

        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.WALKING)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                        .build());

        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.WALKING)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                        .build());

        ActivityTransitionRequest request = new ActivityTransitionRequest(transitions);

        Intent intent = new Intent(ActivityTransitionReceiver.TRANSITION_ACTION_RECEIVER);
        mPendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        mTransitionsReceiver = new ActivityTransitionReceiver();
        registerReceiver(mTransitionsReceiver,
                new IntentFilter(ActivityTransitionReceiver.TRANSITION_ACTION_RECEIVER));

        Task<Void> task =
                ActivityRecognition.getClient(this)
                        .requestActivityTransitionUpdates(request, mPendingIntent);
        task.addOnSuccessListener(
                result -> {
                    
                });
        task.addOnFailureListener(
                e -> {
                    
                });

    }

and created a Receiver class

public class ActivityTransitionReceiver extends BroadcastReceiver {

    public static final String TRANSITION_ACTION_RECEIVER =
            BuildConfig.APPLICATION_ID + "TRANSITION_ACTION_RECEIVER";

    @Override
    public void onReceive(Context context, Intent intent) {

        if (TRANSITION_ACTION_RECEIVER == intent.getAction()) {
            Log.d("DetectActivityReceiver", "Received an unsupported action.");
            return;
        }

        if (ActivityTransitionResult.hasResult(intent)){
            ActivityTransitionResult result = ActivityTransitionResult.extractResult(intent);
            for (ActivityTransitionEvent event : result.getTransitionEvents()){
                System.out.println(event.toString());
            }
        }
    }
}

also added the receiver in Manifest.xml.

Expected to get the event in Receiver class, but didn't.

Tiago Badalo
  • 1
  • 1
  • 2

0 Answers0