0

I’m trying to develop an app using flutter , I want to have all apps usage on the mobile , I have been searching for some packages for this purpose I didn’t find something for this specific purpose.

2
  • Apps can't access to other apps due to security issues that could come out of it. Sep 18, 2020 at 0:21
  • did you find something for iOS>
    – alexlipa
    Jul 9 at 11:25

1 Answer 1

0

You can copy paste run full code below
You can use package https://pub.dev/packages/usage_stats
In working demo you can see package and event timestamp history, then you can do summary
Step 1: AndroidManifest.xml, add xmlns:tools and android.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, see working demo picture at left

working demo

enter image description here

full code

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,
        ),
      ),
    );
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.