android_boot_info 1.0.0 copy "android_boot_info: ^1.0.0" to clipboard
android_boot_info: ^1.0.0 copied to clipboard

PlatformAndroid

A Flutter plugin for Android that provides boot time, uptime, awake time, and deep sleep statistics.

example/lib/main.dart

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: AndroidBootInfoScreen());
  }
}

class AndroidBootInfoScreen extends StatefulWidget {
  const AndroidBootInfoScreen({super.key});

  @override
  State<AndroidBootInfoScreen> createState() => _AndroidBootInfoScreenState();
}

class _AndroidBootInfoScreenState extends State<AndroidBootInfoScreen> {
  /// Fetches boot info values and returns them as a map.
  Future<Map<String, dynamic>> _fetchBootInfo() async {
    /// Forces a fresh snapshot from the native side.
    /// Without calling this, the plugin will return cached values
    /// from the last fetch, so use it when you need updated metrics.
    await AndroidBootInfo.refresh();

    /// Boot time as a [DateTime].
    final DateTime bootTime = await AndroidBootInfo.getBootTime();

    /// Boot time formatted as ISO‑8601 string.
    final String bootTimeISO = await AndroidBootInfo.getBootTimeISO();

    /// Boot time in milliseconds since epoch.
    final int bootTimeMs = await AndroidBootInfo.getBootTimeMs();

    /// Number of times the device has booted.
    final int bootCount = await AndroidBootInfo.getBootCount();

    /// Total uptime since boot as [Duration].
    final Duration uptimeDuration = await AndroidBootInfo.getUptimeDuration();

    /// Uptime in milliseconds.
    final int uptimeMs = await AndroidBootInfo.getUptimeMs();

    /// Uptime in seconds.
    final int uptimeSeconds = await AndroidBootInfo.getUptimeSeconds();

    /// Total awake time since boot as [Duration].
    final Duration awakeDuration = await AndroidBootInfo.getAwakeDuration();

    /// Awake time in milliseconds.
    final int awakeMs = await AndroidBootInfo.getAwakeMs();

    /// Awake time in seconds.
    final int awakeSeconds = await AndroidBootInfo.getAwakeSeconds();

    /// Percentage of uptime spent awake.
    final double awakePercent = await AndroidBootInfo.getAwakePercent();

    /// Total deep sleep time since boot as [Duration].
    final Duration deepSleepDuration =
        await AndroidBootInfo.getDeepSleepDuration();

    /// Deep sleep time in milliseconds.
    final int deepSleepMs = await AndroidBootInfo.getDeepSleepMs();

    /// Deep sleep time in seconds.
    final int deepSleepSeconds = await AndroidBootInfo.getDeepSleepSeconds();

    /// Percentage of uptime spent in deep sleep.
    final double deepSleepPercent = await AndroidBootInfo.getDeepSleepPercent();

    return {
      "bootTimeMs": bootTimeMs,
      "bootTimeISO": bootTimeISO,
      "bootTimeDateTime": bootTime.toIso8601String(),
      "bootCount": bootCount,

      "uptimeDuration": uptimeDuration.toString(),
      "uptimeMs": uptimeMs,
      "uptimeSeconds": uptimeSeconds,

      "awakeDuration": awakeDuration.toString(),
      "awakeMs": awakeMs,
      "awakeSeconds": awakeSeconds,
      "awakePercent": awakePercent,

      "deepSleepDuration": deepSleepDuration.toString(),
      "deepSleepMs": deepSleepMs,
      "deepSleepSeconds": deepSleepSeconds,
      "deepSleepPercent": deepSleepPercent,
    };
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Android Boot Info Example')),
      body: FutureBuilder<Map<String, dynamic>>(
        future: _fetchBootInfo(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(child: CircularProgressIndicator());
          }

          if (snapshot.hasError) {
            return Center(child: Text('Error: ${snapshot.error}'));
          }

          if (!snapshot.hasData) {
            return const Center(child: Text('No data available'));
          }

          final data = snapshot.data!;
          return ListView.separated(
            padding: const EdgeInsets.all(16),
            itemCount: data.length + 1,
            separatorBuilder: (_, _) => const Divider(),
            itemBuilder: (context, index) {
              if (index == data.length) {
                return Padding(
                  padding: const EdgeInsets.only(top: 32.0),
                  child: Center(
                    child: ElevatedButton.icon(
                      onPressed: () {
                        setState(() {});
                      },
                      icon: const Icon(Icons.refresh),
                      label: const Text('Refresh'),
                    ),
                  ),
                );
              }

              final entry = data.entries.elementAt(index);
              return Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Flexible(
                    flex: 1,
                    child: Text(
                      entry.key,
                      style: const TextStyle(fontWeight: FontWeight.bold),
                    ),
                  ),
                  Flexible(
                    flex: 1,
                    child: Text('${entry.value}', textAlign: TextAlign.right),
                  ),
                ],
              );
            },
          );
        },
      ),
    );
  }
}
1
likes
160
points
57
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for Android that provides boot time, uptime, awake time, and deep sleep statistics.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on android_boot_info

Packages that implement android_boot_info