elise-ng/flutter_is_lock_screen
用于检测设备是否处于锁屏状态的 Flutter 插件。有助于判断应用进入后台是由于锁屏还是离开应用。
- Stars
- 4
- Forks
- 34
- 最近推送(UTC)
- 2023年3月13日
- 项目状态
- 未归档
技术话题
使用的依赖
依赖清单 2 项
- flutter
{"sdk":"flutter"} - flutter_test开发依赖
{"sdk":"flutter"}
原始 README
以下为英文项目原文快照,最新内容请访问 GitHub。
展开 / 收起项目 README
is_lock_screen
Detects if device is locked. Useful for determining whether app entered background due to locking screen or leaving app.
Usage
Import library and call the following method.
Note that this only works on physical device for iOS.
bool? result = await isLockScreen();
You will probably observe the AppLifecycleState with a WidgetsBindingObserver and call this when app is in background:
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.inactive) {
print('app inactive, is lock screen: ${await isLockScreen()}');
} else if (state == AppLifecycleState.resumed) {
print('app resumed');
}
}
See example app code for details
Why this plugin?
An alternative to this plugin is hardware_buttons, which uses non-public API (com.apple.springboard.lockcomplete) on iOS to detect lock button usage and violates App Store requirements.
To circumvent this issue, this plugin detects whether iOS device is in lock screen by checking if screen brightness is 0.0 (the user-adjustable minimum is >0.01).
On android, this plugin uses KeyguardManager and PowerManager API to check if device is secured or display is off as suggested in this gist. A similar flow was tested on iOS with LocalAuthentication and UIApplication.shared.isProtectedDataAvailable but failed due to long grace period of the system lock down. The flag always return true the moment screen is locked.