Activity Transition API request activity-transition-updates once

731 Views Asked by At

I am trying to implement activity transition updates in the background of my application. My goal is to request activity transition updates once and get activity transition updates all the time in the background. To achieve this, I implemented the following:

PendingIntent pi = PendingIntent.getBroadcast(mApplicationContext, 100, intent,
                PendingIntent.FLAG_NO_CREATE);
        if (pi == null) {
            pi = PendingIntent.getBroadcast(mApplicationContext, 100,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
            Task<Void> task = ActivityRecognition.getClient(mApplicationContext)
                    .requestActivityTransitionUpdates(atr, pi);
            task.addOnCompleteListener(task1 -> {
                if (task1.isSuccessful()) {
                    Log.v(TAG, "Travel behavior activity-transition-update set up");
                } else {
                    Log.v(TAG, "Travel behavior activity-transition-update failed set up: " +
                            task1.getException().getMessage());
                    task1.getException().printStackTrace();
                }
            });
        }

So, in this code if PendingIntent.getBroadcast(mApplicationContext, 100, intent,PendingIntent.FLAG_NO_CREATE); returns null, I call requestActivityTransitionUpdates method. Otherwise, I don't request, because the activity transition updates are already been requested with the pending intent.

However, this code doesn't work. After the first requestActivityTransitionUpdates, PendingIntent.getBroadcast(mApplicationContext, 100, intent, PendingIntent.FLAG_NO_CREATE) returns a pending intent and I don't call requestActivityTransitionUpdates and I stop getting transition updates.

If I use the following code by removing the PendingIntent.getBroadcast(mApplicationContext, 100, intent, PendingIntent.FLAG_NO_CREATE) line:

PendingIntent pi = PendingIntent.getBroadcast(mApplicationContext, 100,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
            Task<Void> task = ActivityRecognition.getClient(mApplicationContext)
                    .requestActivityTransitionUpdates(atr, pi);
            task.addOnCompleteListener(task1 -> {
                if (task1.isSuccessful()) {
                    Log.v(TAG, "Travel behavior activity-transition-update set up");
                } else {
                    Log.v(TAG, "Travel behavior activity-transition-update failed set up: " +
                            task1.getException().getMessage());
                    task1.getException().printStackTrace();
                }
            });

I keep getting the transition updates. But in this way, I have to request transition updates every time when the app opens. And after each transition update request, the API returns the current activity in my BroadcastReceiver class even though there is no actual activity transition happened.

So, is there a way to request transition updates once and keep getting transition updates all the time?

1

There are 1 best solutions below

0
Mahmoud On

You can use the second way.

But as you say the broadcast receiver will be notified at each app run because the transition API returns the last detected activity.

You can get that event time and ignore it if it's an old one or if it's repeated by saving the last received one in some where such as db or shared preferences.