Detecting user activity in android can de done very easily using ActivityRecognitionClient. You can detect user activities like Still, Running, Walking, Cycling, Tilting, Driving etc., We can see this API widely used in lot of fitness apps (like GoogleFit) to provide user activity info like number steps he is taken, the distance he is travelled.
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<service android:name=".BackgroundDetectedActivitiesService"></service>
dependencies { implementation 'com.android.support:design:27.1.1'
implementation 'com.google.android.gms:play-services-location:11.6.0' }