You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用safe_device检测Flutter开发者模式时遇Settings迁移日志警告

解决safe_device包检测开发者模式时的Android日志警告

问题原因

你看到的这个警告:

W/Settings( 4572): Setting development_settings_enabled has moved from android.provider.Settings.Secure to android.provider.Settings.Global.

是因为你使用的safe_device: ^1.1.4版本,其Android端的开发者模式检测逻辑仍在调用旧的Settings.Secure路径获取设置项,但Android系统已经将development_settings_enabled这个设置迁移到Settings.Global下,所以系统会抛出这个兼容性警告。

解决方案

1. 升级safe_device包到最新版本

这是最简便的解决方式,插件的后续版本应该已经修复了这个API路径问题。修改你的pubspec.yaml文件,将safe_device的版本号更新为最新:

dependencies:
  safe_device: ^最新版本号

然后执行flutter pub get完成依赖更新。

2. 手动修改插件原生代码(无法升级时使用)

如果暂时不能升级包,可以手动修改插件的Android原生实现:

  • 找到项目中safe_device插件的Android模块路径(通常在flutter/.pub-cache/hosted/pub.dartlang.org/safe_device-1.1.4/android/src/main/kotlin/com/example/safe_device/SafeDevicePlugin.kt,路径可能因系统略有不同)
  • 打开对应的检测类,找到获取开发者模式的代码行,将Settings.Secure.getInt(contentResolver, "development_settings_enabled")替换为Settings.Global.getInt(contentResolver, "development_settings_enabled")
  • 保存修改后重新编译项目

额外优化建议

你的代码中用了无限循环每隔5秒轮询检测开发者模式,这种方式会持续占用资源。可以考虑使用Android的ContentObserver监听设置变化,或者只在特定时机(如应用启动、前台切换时)检测,减少不必要的资源消耗。

你的实现代码

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

  @override
  _DeveloperModeCheckerState createState() => _DeveloperModeCheckerState();
}

class _DeveloperModeCheckerState extends State<DeveloperModeChecker> {
  @override
  void initState() {
    super.initState();
    startCheckingDeveloperMode();
  }

  Future<void> startCheckingDeveloperMode() async {
    while (true) {
      try {
        bool developerMode = await SafeDevice.isDevelopmentModeEnable;
        //print("Developer Mode Status: $developerMode");
        if (developerMode == true){
          print("out from the app");
        }
      } catch (error) {
        print("Error: $error");
      }
      await Future.delayed(const Duration(seconds: 5));
    }
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox.shrink();
  }
}
class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: false,
        body: Stack(
          children: [
            const LoginScreen(),
            const DeveloperModeChecker(),
          ],
        ),
      );
  }
}

内容的提问来源于stack exchange,提问作者Andrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 20:42:36