flutter_activity_recognition

Last updated:

0 purchases

flutter_activity_recognition Image
flutter_activity_recognition Images
Add to Cart

Description:

flutter activity recognition

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

Features #

Can request activity recognition permission.
Can subscribe to ActivityStream to listen 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: ^4.0.0
copied to clipboard
After adding the flutter_activity_recognition plugin to the flutter project, we need to specify the platform-specific permissions 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.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
copied to clipboard
🐤 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>
copied to clipboard
How to use #

Checks whether activity recognition permission is granted.

Future<bool> _checkAndRequestPermission() async {
ActivityPermission permission =
await FlutterActivityRecognition.instance.checkPermission();
if (permission == ActivityPermission.PERMANENTLY_DENIED) {
// permission has been permanently denied.
return false;
} else if (permission == ActivityPermission.DENIED) {
permission =
await FlutterActivityRecognition.instance.requestPermission();
if (permission != ActivityPermission.GRANTED) {
// permission is denied.
return false;
}
}

return true;
}
copied to clipboard

To listen activity in real time, use the activityStream getter.

StreamSubscription<Activity>? _activitySubscription;

Future<void> _subscribeActivityStream() async {
if (await _checkAndRequestPermission()) {
_activitySubscription = FlutterActivityRecognition.instance.activityStream
.handleError(_onError)
.listen(_onActivity);
}
}

void _onActivity(Activity activity) {
print('activity detected >> ${activity.toJson()}');
}

void _onError(dynamic error) {
print('error >> $error');
}
copied to clipboard
Background Mode #
If you want to use this plugin in the background, use the flutter_foreground_task plugin.
More Documentation #
Go here to learn about the models provided by this plugin.
Go here to migrate to the new version.
Support #
If you find any bugs or issues while using the plugin, please register an issues on GitHub. You can also contact us at [email protected].

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.