Flutter步数统计插件daily_pedometer的使用
Flutter步数统计插件daily_pedometer的使用
每日步数统计在健康管理中非常重要。本文将介绍如何使用daily_pedometer
插件来实现这一功能。该插件目前仅支持Android平台。
权限设置
为了使daily_pedometer
插件正常工作,你需要在项目的AndroidManifest.xml
文件中添加以下权限:
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
示例用法
你可以查看示例应用以获得一个完整的例子。下面是一个更通用的例子。记得设置所需的权限,这可能需要你在手机的“设置”中手动允许。
import 'dart:io';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:daily_pedometer/daily_pedometer.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeService();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = '未知';
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// 初始化平台状态
Future<void> initPlatformState() async {
final pedometer = DailyPedometer();
await pedometer.initialize(false);
setState(() {
_platformVersion = " ${pedometer.steps} 步";
});
pedometer.dailyStepCountStream.listen((event) async {
setState(() {
print("stepCountStream : $event");
_platformVersion = " $event 步";
});
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('插件示例应用'),
),
body: Center(
child: Text('运行于: $_platformVersion\n'),
),
),
);
}
}
const notificationChannelId = 'my_foreground';
Future<void> initializeService() async {
if (!Platform.isAndroid) return;
final service = FlutterBackgroundService();
service.invoke("refreshSensorListener");
// 创建通知通道
const AndroidNotificationChannel channel = AndroidNotificationChannel(
notificationChannelId, // id
'我的前台服务', // 标题
description: '此通道用于重要的通知。', // 描述
importance: Importance.low, // 重要性必须为低或更高
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
if (Platform.isIOS || Platform.isAndroid) {
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
iOS: DarwinInitializationSettings(),
android: AndroidInitializationSettings('ic_bg_service_small'),
),
);
}
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
await service.configure(
androidConfiguration: AndroidConfiguration(
onStart: onStart,
autoStart: true,
isForegroundMode: true,
notificationChannelId: notificationChannelId,
initialNotificationTitle: '优秀服务',
initialNotificationContent: '初始化',
foregroundServiceNotificationId: 888,
),
iosConfiguration: IosConfiguration(
autoStart: true,
onForeground: onStart,
),
);
}
// 启动服务时调用
[@pragma](/user/pragma)('vm:entry-point')
void onStart(ServiceInstance service) async {
DartPluginRegistrant.ensureInitialized();
print("111111 : 11");
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
if (service is AndroidServiceInstance) {
service.on('setAsForeground').listen((event) {
print("111111 : 1");
service.setAsForegroundService();
});
service.on('setAsBackground').listen((event) {
print("111111 : 2");
service.setAsBackgroundService();
});
}
service.on('stopService').listen((event) {
print("111111 : 3");
service.stopSelf();
});
DailyPedometer pedometer = DailyPedometer();
service.on("refreshSensorListener").listen((event) async {
print("refreshSensorListener");
await pedometer.refreshSensorListener();
});
if (service is AndroidServiceInstance) {
await pedometer.initialize(true);
print("DailyPedometer 前台服务 ${pedometer.steps}");
flutterLocalNotificationsPlugin.show(
888,
'测试步数',
'今天的步数: ${pedometer.steps}',
const NotificationDetails(
android: AndroidNotificationDetails(
notificationChannelId, notificationChannelId,
icon: 'ic_bg_service_small', ongoing: true),
),
);
pedometer.dailyStepCountStream.listen((event) async {
print("DailyPedometer 前台服务 $event");
flutterLocalNotificationsPlugin.show(
888,
'测试步数',
'今天的步数: $event',
const NotificationDetails(
android: AndroidNotificationDetails(
notificationChannelId, notificationChannelId,
icon: 'ic_bg_service_small', ongoing: true),
),
);
});
}
}
更多关于Flutter步数统计插件daily_pedometer的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复