This plugin is used to recognize user activity on Android and iOS platforms. To implement this plugin, Android used ActivityRecognitionClient and iOS used CMMotionActivityManager.

pub package

Features

  • Can check or request activity recognition permission.
  • Subscribe to an activity stream to detect user activity in real time.

Getting started

To use this plugin, add flutter_activity_recognition as a dependency in your pubspec.yaml file. For example:

dependencies:
  flutter_activity_recognition: ^1.0.0

After adding the flutter_activity_recognition plugin to the flutter project, we need to specify the platform-specific permissions and services to use for this plugin to work properly.

:baby_chick: Android

Open the AndroidManifest.xml file and add the following permissions between the <manifest> and <application> tags.

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

And specify the service and receiver inside the <application> tag as follows.

<receiver
    android:name="com.pravera.flutter_activity_recognition.service.ActivityRecognitionIntentReceiver" />
<service
    android:name="com.pravera.flutter_activity_recognition.service.ActivityRecognitionIntentService"
    android:permission="android.permission.BIND_JOB_SERVICE"
    android:stopWithTask="true" />

:baby_chick: iOS

Open the ios/Runner/Info.plist file and add the following permission inside the <dict> tag.

<key>NSMotionUsageDescription</key>
<string>Used to recognize user activity information.</string>

How to use

  1. Create a FlutterActivityRecognition instance.
final activityRecognition = FlutterActivityRecognition.instance;
  1. Checks whether activity recognition permission is granted.
Future<bool> isPermissionGrants() async {
  // Check if the user has granted permission. If not, request permission.
  PermissionRequestResult reqResult;
  reqResult = await activityRecognition.checkPermission();
  if (reqResult == PermissionRequestResult.PERMANENTLY_DENIED) {
    dev.log('Permission is permanently denied.');
    return false;
  } else if (reqResult == PermissionRequestResult.DENIED) {
    reqResult = await activityRecognition.requestPermission();
    if (reqResult != PermissionRequestResult.GRANTED) {
      dev.log('Permission is denied.');
      return false;
    }
  }

  return true;
} 
  1. Subscribe to an activity stream to receive activity data in real time.
// Subscribe to the activity stream.
final activityStreamSubscription = activityRecognition.getActivityStream()
  .handleError(_handleError).listen(_onActivityReceive);
  1. When the widget is dispose or the plugin is finished using, cancel the subscription.
@override
void dispose() {
  activityStreamSubscription?.cancel();
  super.dispose();
}

Models

:chicken: PermissionRequestResult

Defines the type of permission request result.

ValueDescription
GRANTEDOccurs when the user grants permission.
DENIEDOccurs when the user denies permission.
PERMANENTLY_DENIEDOccurs when the user denies the permission once and chooses not to ask again.

:chicken: Activity

A model representing the user's activity.

PropertyDescription
typeThe type of activity recognized.
confidenceThe confidence of activity recognized.

:chicken: ActivityType

Defines the type of activity.

ValueDescription
IN_VEHICLEThe device is in a vehicle, such as a car.
ON_BICYCLEThe device is on a bicycle.
ON_FOOTThe device is on a user who is walking or running.
RUNNINGThe device is on a user who is running. This is a sub-activity of ON_FOOT.
STILLThe device is still (not moving).
TILTINGThe device angle relative to gravity changed significantly. This often occurs when a device is picked up from a desk or a user who is sitting stands up.
WALKINGThe device is on a user who is walking. This is a sub-activity of ON_FOOT.
UNKNOWNUnable to detect the current activity.

:chicken: ActivityConfidence

Defines the confidence of activity.

ValueDescription
HIGHHigh accuracy: 75~100
MEDIUMMedium accuracy: 50~75
LOWLow accuracy: 0~50

Support

If you find any bugs or issues while using the plugin, please register an issues on GitHub. You can also contact us at hwj930513@naver.com.

Libraries

activity
activity_confidence
activity_type
flutter_activity_recognition
permission_request_result