-1

我正在尝试使用 flutter 开发一个应用程序,我想在移动设备上使用所有应用程序,我一直在为此目的搜索一些包我没有找到用于这个特定目的的东西。

4

1 回答 1

0

您可以在下面复制粘贴运行完整代码
您可以使用包https://pub.dev/packages/usage_stats
在工作演示中您可以查看packageevent timestamp历史记录,然后您可以进行摘要
步骤 1: AndroidManifest.xml,添加xmlns:toolsandroid.permission.PACKAGE_USAGE_STATS

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your package name" xmlns:tools="http://schemas.android.com/tools">

<uses-permission
    android:name="android.permission.PACKAGE_USAGE_STATS"
    tools:ignore="ProtectedPermissions" />

Step 2: Permit usage access, 见左侧工作演示图片

工作演示

在此处输入图像描述

完整代码

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:usage_stats/usage_stats.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<EventUsageInfo> events = [];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      initUsage();
    });
  }

  Future<void> initUsage() async {
    UsageStats.grantUsagePermission();
    DateTime endDate = new DateTime.now();
    DateTime startDate = DateTime(2020, 1, 1, 0, 0, 0);

    List<EventUsageInfo> queryEvents =
        await UsageStats.queryEvents(startDate, endDate);

    this.setState(() {
      events = queryEvents.reversed.toList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text("Usage Stats"),
        ),
        body: Container(
            child: ListView.separated(
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(events[index].packageName),
                    subtitle: Text(
                        "Last time used: ${DateTime.fromMillisecondsSinceEpoch(int.parse(events[index].timeStamp)).toIso8601String()}"),
                    trailing: Text(events[index].eventType),
                  );
                },
                separatorBuilder: (context, index) => Divider(),
                itemCount: events.length)),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            initUsage();
          },
          child: Icon(
            Icons.refresh,
          ),
          mini: true,
        ),
      ),
    );
  }
}
于 2020-09-18T02:31:10.837 回答