activity_rec_flutter

This plugin is used to recognize user activity on Android and iOS platforms.


License
MIT

Documentation

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 activity_rec_flutter as a dependency in your pubspec.yaml file. For example:

dependencies:
  activity_rec_flutter: ^1.0.0

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

🐤 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" />

🐤 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.activityStream
  .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

🐔 PermissionRequestResult

Defines the type of permission request result.

Value Description
GRANTED Occurs when the user grants permission.
DENIED Occurs when the user denies permission.
PERMANENTLY_DENIED Occurs when the user denies the permission once and chooses not to ask again.

🐔 Activity

A model representing the user's activity.

Property Description
type The type of activity recognized.
confidence The confidence of activity recognized.

🐔 ActivityType

Defines the type of activity.

Value Description
IN_VEHICLE The device is in a vehicle, such as a car.
ON_BICYCLE The device is on a bicycle.
RUNNING The device is on a user who is running. This is a sub-activity of ON_FOOT.
STILL The device is still (not moving).
WALKING The device is on a user who is walking. This is a sub-activity of ON_FOOT.
UNKNOWN Unable to detect the current activity.

🐔 ActivityConfidence

Defines the confidence of activity.

Value Description
HIGH High accuracy: 80~100
MEDIUM Medium accuracy: 50~80
LOW Low 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.