如何将 Flutter 应用锁定到 Kiosk 应用?

Nic*_*ger 7 android flutter

我最近开始将一个 Android 应用程序迁移到 Flutter。我喜欢 Flutter Apps 的编码方式。2 天后,Flutter 应用程序具有与 Android 应用程序相同的功能。唯一缺少的功能是锁定。Android 应用程序在合作伙伴拥有的设备上运行并且是设备所有者。锁定是通过以下方法实现的:

    private void setDefaultCosuPolicies(boolean active) {
    // set user restrictions
    setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, active);
    setUserRestriction(UserManager.DISALLOW_FACTORY_RESET, active);
    setUserRestriction(UserManager.DISALLOW_ADD_USER, active);
    setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
    setUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME, active);

    // disable keyguard and status bar
    mDevicePolicyManager.setKeyguardDisabled(mAdminName, active);
    mDevicePolicyManager.setStatusBarDisabled(mAdminName, active);

    // enable STAY_ON_WHILE_PLUGGED_IN
    enableStayOnWhilePluggedIn(false);

    // set this Activity as a lock task package

    mDevicePolicyManager.setLockTaskPackages(mAdminName,
            active ? new String[]{getPackageName()} : new String[]{});

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
    intentFilter.addCategory(Intent.CATEGORY_HOME);
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);

    if (active) {
      // set Cosu activity as home intent receiver so that it is started
      // on reboot
      mDevicePolicyManager.addPersistentPreferredActivity(
              mAdminName, intentFilter, new ComponentName(
                      getPackageName(), MainActivity.class.getName()));
    } else {
      mDevicePolicyManager.clearPackagePersistentPreferredActivities(
              mAdminName, getPackageName());
    }
  }

  private void setUserRestriction(String restriction, boolean disallow) {
    if (disallow) {
      mDevicePolicyManager.addUserRestriction(mAdminName,
              restriction);
    } else {
      mDevicePolicyManager.clearUserRestriction(mAdminName,
              restriction);
    }
  }

  private void enableStayOnWhilePluggedIn(boolean enabled) {
    if (enabled) {
      mDevicePolicyManager.setGlobalSetting(
              mAdminName,
              Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
              Battery_PLUGGED_ANY);
    } else {
      mDevicePolicyManager.setGlobalSetting(
              mAdminName,
              Settings.Global.STAY_ON_WHILE_PLUGGED_IN, DONT_STAY_ON);
    }
  }
Run Code Online (Sandbox Code Playgroud)

在 MainActivitys onCreate 函数 setDefaultCosuPolicies(true) 被调用。我将这些函数复制到 flutter 应用程序中。添加了 AdminReceiver 并在 AndroidManifest 中配置了所有内容。第一次尝试没有成功,所以我尝试通过 MethodChannel 调用 setDefaultCosuPolicies 函数。这也不起作用。

有谁知道如何激活信息亭功能?

提前致谢

Tas*_*hin 1

在项目中添加此包:

  kiosk_mode: ^0.2.1
Run Code Online (Sandbox Code Playgroud)

例子:

import 'dart:io';

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

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

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: const Center(child: _Home()),
        ),
      );
}

class _Home extends StatefulWidget {
  const _Home({
    Key? key,
  }) : super(key: key);

  @override
  State<_Home> createState() => _HomeState();
}

class _HomeState extends State<_Home> {
  late final Stream<KioskMode> _currentMode = watchKioskMode();

  @override
  Widget build(BuildContext context) => Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (Platform.isAndroid) ...[
            const MaterialButton(
              onPressed: startKioskMode,
              child: Text('Start Kiosk Mode'),
            ),
            const MaterialButton(
              onPressed: stopKioskMode,
              child: Text('Stop Kiosk Mode'),
            ),
          ],
          MaterialButton(
            onPressed: () => getKioskMode().then(
              (value) => ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(content: Text('Kiosk mode: $value')),
              ),
            ),
            child: const Text('Check mode'),
          ),
          StreamBuilder<KioskMode>(
            stream: _currentMode,
            builder: (context, snapshot) => Text(
              'Current mode: ${snapshot.data}',
            ),
          ),
        ],
      );
}
Run Code Online (Sandbox Code Playgroud)