Flutter前台保活插件android_foreground_alive的使用
android_foreground_alive
android_foreground_alive
是一个用于在 Flutter 中实现前台保活功能的插件。通过此插件,应用程序可以在后台运行时保持活跃状态,避免被系统杀掉。
Getting Started
本项目是一个 Flutter 插件包的起点,包含针对 Android 和/或 iOS 的平台特定实现代码。
要开始使用 Flutter,请查看 Flutter 官方文档,其中提供了教程、示例、移动开发指南以及完整的 API 参考。
使用示例
以下是一个完整的示例代码,展示如何使用 android_foreground_alive
插件来实现前台保活功能。
示例代码
// example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart'; // 引入 Flutter 原生插件支持
import 'package:android_foreground_alive/android_foreground_alive.dart'; // 引入前台保活插件
void main() {
runApp(MyApp()); // 启动应用
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState(); // 创建状态类
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown'; // 保存平台版本信息
@override
void initState() {
super.initState();
initPlatformState(); // 初始化插件状态
}
// 初始化插件状态
Future<void> initPlatformState() async {
String platformVersion;
try {
// 启动前台保活服务
await AndroidForegroundAlive.startForegroundAlive;
// 获取平台版本信息
platformVersion = await AndroidForegroundAlive.platformVersion;
} on PlatformException {
// 如果获取失败,返回错误信息
platformVersion = 'Failed to get platform version.';
}
// 如果界面已经卸载,则不更新状态
if (!mounted) return;
// 更新 UI 界面
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp( // 构建 Material UI 应用
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'), // 设置标题
),
body: Center(
child: Text('Running on: $_platformVersion\n'), // 显示平台版本信息
),
),
);
}
}
运行效果
运行上述代码后,应用程序会启动一个前台服务,并显示当前运行的平台版本信息。即使应用程序切换到后台,前台服务仍然会保持运行状态。
注意事项
- 权限配置:
在 Android 平台中,需要在
AndroidManifest.xml
文件中添加必要的权限和前台服务声明。
并在服务声明中添加:<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<service android:name=".foreground.MyForegroundService" android:foregroundServiceType="dataSync" android:exported="false"/>
更多关于Flutter前台保活插件android_foreground_alive的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter前台保活插件android_foreground_alive的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
android_foreground_alive
是一个 Flutter 插件,用于在 Android 平台上实现前台服务的保活功能。通过使用该插件,你可以在应用进入后台时保持应用的活动状态,防止系统杀死应用进程。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 android_foreground_alive
插件的依赖:
dependencies:
flutter:
sdk: flutter
android_foreground_alive: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装插件。
使用插件
1. 初始化插件
在使用插件之前,你需要在 main.dart
文件中初始化插件:
import 'package:android_foreground_alive/android_foreground_alive.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await AndroidForegroundAlive.init();
runApp(MyApp());
}
2. 启动前台服务
在需要保活的地方,调用 startForegroundService
方法启动前台服务:
import 'package:android_foreground_alive/android_foreground_alive.dart';
void startForegroundService() async {
await AndroidForegroundAlive.startForegroundService(
title: 'My App',
content: 'App is running in the background',
icon: 'ic_notification', // 通知图标资源名称
);
}
icon
参数是通知图标的资源名称,你需要在 android/app/src/main/res/drawable
目录下放置相应的图标文件(例如 ic_notification.png
)。
3. 停止前台服务
当不再需要保活时,可以调用 stopForegroundService
方法停止前台服务:
void stopForegroundService() async {
await AndroidForegroundAlive.stopForegroundService();
}
4. 处理生命周期
你可以在 WidgetsBindingObserver
中监听应用的生命周期,以在应用进入后台时启动前台服务,并在应用回到前台时停止前台服务:
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
[@override](/user/override)
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
[@override](/user/override)
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
[@override](/user/override)
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
startForegroundService();
} else if (state == AppLifecycleState.resumed) {
stopForegroundService();
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Foreground Service Example'),
),
body: Center(
child: Text('Keep app alive in the background'),
),
),
);
}
}
注意事项
-
权限:在 Android 10 及以上版本,你需要在
AndroidManifest.xml
中添加FOREGROUND_SERVICE
权限:<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
-
通知图标:确保你提供的通知图标资源存在于
drawable
目录中,并且名称与代码中的icon
参数一致。 -
后台限制:Android 系统对后台应用的活动有一定的限制,尤其是在 Android 8.0(API 26)及以上版本。前台服务可以帮助你绕过这些限制,但仍然需要遵循 Android 的最佳实践。
-
电池优化:长时间运行的前台服务可能会影响设备的电池寿命,建议在不需要时及时停止服务。
示例代码
以下是一个完整的示例代码:
import 'package:flutter/material.dart';
import 'package:android_foreground_alive/android_foreground_alive.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await AndroidForegroundAlive.init();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
[@override](/user/override)
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
[@override](/user/override)
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
[@override](/user/override)
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
startForegroundService();
} else if (state == AppLifecycleState.resumed) {
stopForegroundService();
}
}
void startForegroundService() async {
await AndroidForegroundAlive.startForegroundService(
title: 'My App',
content: 'App is running in the background',
icon: 'ic_notification',
);
}
void stopForegroundService() async {
await AndroidForegroundAlive.stopForegroundService();
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Foreground Service Example'),
),
body: Center(
child: Text('Keep app alive in the background'),
),
),
);
}
}