How to control other app through Flutter app?


I spend a lot of time on YouTube every day. This is very alarming for me, and this is why I want to build a Flutter app where the time phone usage will be calculated, and after 2 hours of using the phone in a day, my YouTube will be locked for the whole day, and it will be unlocked the next day.

Looking for experts' suggestions.

Is it a realistic and feasible idea? If yes, then how may I implement?
Please share your experience and plan.

1
Apr 5 at 7:11 AM
User AvatarKafiul Islam
#android#flutter#background-service#auto-lock

Accepted Answer

Yes, this is feasible, but the implementation differs significantly between Android and iOS due to platform restrictions.

Platform Reality Check

Android ✅ Fully possible iOS ⚠️ Heavily restricted (Apple's sandboxing prevents most of this)

For a practical app, target Android first.


How It Works — The Technical Stack

Flutter itself can't control other apps directly. You need native Android APIs bridged via Flutter's MethodChannel. Here's the architecture:

1. Screen Time / Usage Tracking

Use Android's UsageStatsManager API:

// Android (Kotlin) - Native side
val usageStatsManager = context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val stats = usageStatsManager.queryUsageStats(
    UsageStatsManager.INTERVAL_DAILY, startTime, endTime
)
// Filter for YouTube: "com.google.android.youtube"

Requires permission: android.permission.PACKAGE_USAGE_STATS (user must grant manually in Settings).

2. App Blocking — Two Approaches

Option A: Accessibility Service (Recommended)

  • Your app runs a background AccessibilityService

  • When it detects YouTube is in the foreground, it immediately launches your lock screen on top

  • This is how apps like Digital Wellbeing, ActionDash, and StayFree work

class AppBlockerService : AccessibilityService() {
    override fun onAccessibilityEvent(event: AccessibilityEvent) {
        if (event.packageName == "com.google.android.youtube") {
            if (isYouTubeLocked()) {
                launchLockScreen() // Bring your Flutter app to front
            }
        }
    }
}

Option B: Android Device Policy (MDM) — More powerful but complex

  • Requires enrolling as a Device Admin or using DevicePolicyManager

  • Can hard-block apps entirely

  • Overkill for personal use

3. Flutter ↔ Native Bridge

// Flutter (Dart) side
class UsageService {
  static const _channel = MethodChannel('com.yourapp/usage');

  Future<int> getYouTubeUsageMinutes() async {
    final int minutes = await _channel.invokeMethod('getYouTubeUsage');
    return minutes;
  }

  Future<void> setYouTubeLocked(bool locked) async {
    await _channel.invokeMethod('setLocked', {'locked': locked});
  }
}

4. Persistence & Daily Reset

// Use shared_preferences to store lock state
final prefs = await SharedPreferences.getInstance();
final today = DateTime.now().toIso8601String().substring(0, 10); // "2026-04-05"
final lockedDate = prefs.getString('locked_date');

if (lockedDate != today) {
  // New day — unlock YouTube
  prefs.remove('locked_date');
}

Recommended Package Stack

PurposePackageNative bridgeflutter/services.dart (MethodChannel)Local storageshared_preferencesBackground tasksflutter_background_serviceNotificationsflutter_local_notificationsPermissionspermission_handler


Implementation Plan (Step-by-Step)

Phase 1 — Usage Detection

  1. Set up UsageStatsManager on Android native side

  2. Bridge it to Flutter via MethodChannel

  3. Display today's phone usage in your UI

Phase 2 — Lock Logic

  1. Build an AccessibilityService in Kotlin

  2. Monitor foreground app every second

  3. If YouTube + locked → push your overlay/lock screen

Phase 3 — Flutter UI

  1. Dashboard showing daily usage

  2. Lock screen overlay (a full-screen Flutter route)

  3. Daily reset logic at midnight

Phase 4 — Polish

  1. Background service that survives app close

  2. Prevent easy bypasses (disable uninstall during lock, PIN to change settings)


Existing Apps to Study / Use as Reference

  • Digital Wellbeing (built into Android) — same concept

  • StayFree (open source portions available)

  • AppBlock — uses identical AccessibilityService approach


Honest Caveats

  • The user must manually grant Accessibility + Usage Stats permissions — Android security requirement, no way around it

  • A determined person can bypass it by uninstalling the app or using YouTube in a browser

  • iOS version would be limited to Screen Time API (read-only) — you can't programmatically lock apps on iOS without MDM enrollment

  • Even if you built the app, Google playstore / iOS app store would not approve the app. I can see a high chance of rejection

This is a well-trodden path — Digital Wellbeing apps use exactly this architecture. You're essentially building a lite version of that.

User AvatarMuhamed Riyas M
Apr 5 at 8:38 PM
1