Flutter应用管理插件apps_bouncer的使用

Flutter应用管理插件apps_bouncer的使用

Apps Bouncer

apps-bouncer 是一个应用程序,用于跟踪正在运行的进程,并将那些行为不端的进程举报给用户,用户可以选择杀死这些进程。

为什么使用它?

想象一下,你的操作系统就像一个酒吧,而正在运行的进程就像是里面的顾客。apps-bouncer 就像是这个酒吧的保镖,当顾客行为不当的时候,他们会受到经理(即用户)的关注。用户可以决定是否要结束这些进程。这是合法的,因为结束进程完全是被允许的;)。

支持的平台

目前只有 MacOS 完全支持。

在 MacOS 上,通知器是一个弹出窗口,看起来像这样:

MacOS Notifier

在其他类 Unix 系统(如 Linux)上,代码应该可以工作,但唯一的通知实现是在终端打印信息。

在 Windows 和其他操作系统上,需要替换 ps 命令,并且如果命令输出格式与 ps 不同,则还需要解析命令输出。

下载

你可以从 GitHub Releases 页面下载预构建的可执行文件。

如果你是 Dart 用户,只需运行以下命令:

dart pub global activate apps_bouncer

要自行构建,参见后面的 “Building” 部分。

配置

要配置 apps-bouncer,可以在以下位置之一提供配置文件:

  • $HOME/.config/apps-bouncer/config.yaml
  • ./apps-bouncer.yaml
  • 通过传递参数到 apps_bouncer 来提供另一个路径。

配置文件的内容如下(所有值都是可选的):

# 采样操作系统的周期(秒),最小值为 1,最大值为 360,默认为 2
periodSeconds: 5

# 进程可能行为不当的最大次数(即超过限制)
# 最小值为 1,最大值为 100,默认为 4
misbehavingChances: 5

# CPU 使用率阈值(百分比),最小值为 1,最大值为 100,默认为 50
cpuThreshold: 75
  
# 内存使用率阈值(百分比),最小值为 1,最大值为 100,默认为 25
memoryThreshold: 50
  
# 在此期间内不会再次通知用户有关行为不当的进程(分钟)
# 最小值为 1,最大值为 30 * 24 * 60,默认为 60
postNotificationPeriodMinutes: 30

# 日志级别(日志发送到标准输出)
# 可以是 'finer', 'fine', 'info', 'warning', 'error'
# 默认为 'info'
logLevel: warning

构建

该项目使用 Dartle 构建。

要获取 Dartle,请运行:

dart pub global activate dartle

然后在根目录下运行 dartle 来构建和测试项目。

要创建可执行文件,请运行:

dartle compileExe

可执行文件将保存在 build/bin/apps_bouncer

示例代码

以下是 apps_bouncer 的使用示例:

import 'package:apps_bouncer/apps_bouncer.dart' as bouncer;
import 'package:yaml/yaml.dart';

void main() async {
  // 加载配置文件
  final yaml = loadYaml('''
  periodSeconds: 5
  logLevel: error
  ''');

  // 将 YAML 转换为配置对象
  final config = bouncer.BouncerConfig.fromJson(yaml);
  print(config);

  // 运行 apps_bouncer
  await bouncer.run(config);
}

更多关于Flutter应用管理插件apps_bouncer的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter应用管理插件apps_bouncer的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter应用中,apps_bouncer插件可以帮助你管理应用的启动行为,特别是在处理应用被系统或其他应用(如任务管理器)终止后的重启行为。以下是如何在Flutter项目中使用apps_bouncer插件的一个基本示例。

首先,确保你已经在pubspec.yaml文件中添加了apps_bouncer依赖:

dependencies:
  flutter:
    sdk: flutter
  apps_bouncer: ^x.y.z  # 替换为最新的版本号

然后运行flutter pub get来安装依赖。

接下来,在你的Flutter项目的main.dart文件中,你可以按照以下步骤来使用apps_bouncer插件:

import 'package:flutter/material.dart';
import 'package:apps_bouncer/apps_bouncer.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Apps Bouncer Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _isBouncerEnabled = false;

  @override
  void initState() {
    super.initState();
    _initializeBouncer();
  }

  Future<void> _initializeBouncer() async {
    bool isAvailable = await AppsBouncer.isAvailable();
    if (isAvailable) {
      bool isEnabled = await AppsBouncer.isEnabled();
      setState(() {
        _isBouncerEnabled = isEnabled;
      });
    } else {
      // 处理不支持的情况,可能需要提示用户或采取其他措施
      print("Apps Bouncer is not available on this device.");
    }
  }

  Future<void> _toggleBouncer() async {
    bool isAvailable = await AppsBouncer.isAvailable();
    if (isAvailable) {
      bool isEnabled = await AppsBouncer.isEnabled();
      await AppsBouncer.setEnabled(!isEnabled);
      setState(() {
        _isBouncerEnabled = !isEnabled;
      });
    } else {
      // 处理不支持的情况
      print("Apps Bouncer is not available on this device.");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Apps Bouncer Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Apps Bouncer Enabled: $_isBouncerEnabled',
              style: TextStyle(fontSize: 24),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _toggleBouncer,
              child: Text('Toggle Apps Bouncer'),
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中,我们做了以下几件事情:

  1. pubspec.yaml文件中添加了apps_bouncer依赖。
  2. main.dart文件中导入了apps_bouncer包。
  3. _MyHomePageState类中,使用AppsBouncer.isAvailable()检查设备是否支持Apps Bouncer功能。
  4. 使用AppsBouncer.isEnabled()检查Apps Bouncer是否已启用。
  5. 提供了一个按钮来切换Apps Bouncer的启用状态。

请注意,Apps Bouncer的功能可能依赖于设备的操作系统版本和制造商的实现,因此在实际应用中,你可能需要处理更多的边缘情况和用户提示。此外,确保在发布应用前测试各种设备和场景,以确保功能的稳定性和兼容性。

回到顶部