zo_app_blocker 0.0.2 copy "zo_app_blocker: ^0.0.2" to clipboard
zo_app_blocker: ^0.0.2 copied to clipboard

PlatformAndroid

A Flutter plugin to block specific applications on Android.

zo_app_blocker #

Pub Version License: MIT

A Flutter plugin to block specific applications on Android.

Under the hood, it leverages Android's AccessibilityService and ForegroundService. This ensures that the app blocking mechanism remains persistent and active, even if the user swipes your Flutter app away from their recent apps list.

Note: This package currently supports Android only.


Features #

  • Targeted Blocking: Specify exactly which installed applications should be blocked from launching.
  • Persistent Execution: Utilizes a Foreground Service to ensure the background blocking process is not killed by the system.
  • App Discovery: Easily fetch a list of all installed applications on the device.

Get Started #

Add zo_app_blocker to your pubspec.yaml:

dependencies:
  zo_app_blocker: ^latest_version

Or install it via the command line:

flutter pub add zo_app_blocker

Setup & Permissions (Android) #

To get this plugin working, several Android permissions need to be handled.

1. AndroidManifest.xml #

Ensure you add the following permissions to your app's android/app/src/main/AndroidManifest.xml file, directly inside the <manifest> tag:

    <!-- Allows querying installed packages -->
    <uses-permission
        android:name="android.permission.QUERY_ALL_PACKAGES"
        tools:ignore="QueryAllPackagesPermission" />

    <!-- Required for the background monitoring service -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
    
    <!-- Required to show notifications on Android 13+ -->
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

2. Notification Permission (Android 13+) #

If you're targeting Android 13 (API level 33) or higher, you must ask the user for permission to show notifications before you can start the background service. You can use the plugin's built-in methods:

import 'package:zo_app_blocker/zo_app_blocker.dart';

Future<void> handleNotificationPermission() async {
  final status = await ZoAppBlocker.instance.checkNotificationPermission();
  if (status != 'granted') {
    await ZoAppBlocker.instance.requestNotificationPermission();
  }
}

3. Accessibility Permission #

For the plugin to actually detect when a blocked app is opened, the user has to go into their Android settings and enable Accessibility for your app. You can check the status and prompt them to open settings like this:

import 'package:zo_app_blocker/zo_app_blocker.dart';

// ...

final status = await ZoAppBlocker.instance.checkAccessibilityPermission();
if (status == 'denied') {
  await ZoAppBlocker.instance.requestAccessibilityPermission(); // Takes them to Android settings
}

Usage #

1. Style your block screen #

Before you block anything, you may want to customize the screen that shows up. You can also customize the background notification here.

import 'package:zo_app_blocker/zo_app_blocker.dart';

// ...

await ZoAppBlocker.instance.setBlockScreenConfig(
  backgroundColor: '#F44336', // Hex color strings
  title: 'Stop Right There!',
  titleColor: '#FFFFFF',
  description: 'You blocked this app. Get back to work!',
  descriptionColor: '#DDDDDD',
  notificationTitle: 'Focus Mode Active', 
  notificationDescription: 'Monitoring your apps in the background.'
);

2. Get a list of installed apps #

If you need to show the user a list of apps they can block, you can fetch all installed apps:

import 'package:zo_app_blocker/zo_app_blocker.dart';

// ...

final installedApps = await ZoAppBlocker.instance.getApps();
// Returns a list containing maps, e.g.:
// [{"packageName": "com.facebook.katana", "appName": "Facebook"}, ...]

3. Start blocking #

Just pass a list of package names to the plugin. Once you call this, the Foreground Service starts up automatically.

import 'package:zo_app_blocker/zo_app_blocker.dart';

// ...

await ZoAppBlocker.instance.blockApps([
  'com.instagram.android',
  'com.facebook.katana',
]);

4. Unblock apps #

You can remove specific apps from the blocklist, or just unblock everything (which also stops the background service).

import 'package:zo_app_blocker/zo_app_blocker.dart';

// ...

// Unblock specific ones
await ZoAppBlocker.instance.unblockApps(['com.facebook.katana']);

// Unblock everything and stop the service
await ZoAppBlocker.instance.unblockAll();

Feel free to post a feature requests or report a bug issues.

My Other packages #

  • zo_animated_border: A package that provides a modern way to create gradient borders with animation in Flutter

  • zo_micro_interactions: A curated set of high-quality Flutter micro-interactions designed for modern, polished apps.

  • zo_screenshot: The zo_screenshot plugin helps restrict screenshots and screen recording in Flutter apps, enhancing security and privacy by preventing unauthorized screen captures.

  • zo_collection_animation: A lightweight Flutter package to create smooth collect animations for coins carts

  • connectivity_watcher: A Flutter package to monitor internet connectivity with subsecond response times, even on mobile networks.

  • ultimate_extension: Enhances Dart collections and objects with utilities for advanced data manipulation and simpler coding.

  • theme_manager_plus: Allows customization of your app's theme with your own theme class, eliminating the need for traditional

  • date_util_plus: A powerful Dart API designed to augment and simplify date and time handling in your Dart projects.

  • pick_color: A Flutter package that allows you to extract colors and hex codes from images with a simple touch.

1
likes
160
points
68
downloads

Documentation

API reference

Publisher

verified publisherthezerone.com

Weekly Downloads

A Flutter plugin to block specific applications on Android.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on zo_app_blocker

Packages that implement zo_app_blocker