Search code examples
androidactivity-recognitionactivitytransitionapi

Android Transitions API - stops receiving any updates after 24-48 hours


I'm using Transitions API and everything works fine for the first 24-48 hours - I receive physical activity changes from the API, but after 24-48 hours there's nothing.

Under Transitions API Receiver, I use Foreground Service that is still alive after those 48 hours. I can verify that by seeing logs; additionally, Location (Fused Location Client) updates are still coming in after those 24-48 hours whereas Transition API seems to be dead.

Has anyone experienced this? I do think that maybe resetting (unregister and then register again) Transitions API using Worker might be alternative (every 1-2 hrs). Not sure if this would help. I have a feeling this happens when app enters Doze mode/some kind of sleep state.

Also, my foreground service uses exported=false, maybe setting it to true would help? (I would love for someone to give an answer and explaining why you should use one or another)

OS: Android 12


Solution

  • On devices that have significant motion sensor, the battery is conserved by stopping activity reporting when the device is still for an extended period of time. It'll resume once devices move again.

    This is mention in Google Developer Video

    UPDATE:

    Motion is considered a device movement, such as tilt, shake, rotation, or swing. And it must ne significant (not small) motion. you can test it in this way.

    private SensorManager sensorManager;
    private Sensor sensor;
    private TriggerEventListener triggerEventListener;
    ...
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
    
    triggerEventListener = new TriggerEventListener() {
        @Override
        public void onTrigger(TriggerEvent event) {
            // Do work
        }
    };
    
    sensorManager.requestTriggerSensor(triggerEventListener, mSensor);