提问人:Muhammad Arsalan 提问时间:11/12/2023 更新时间:11/12/2023 访问量:31
当应用在带有包或方法通道的 flutter 中处于 backgorund/inactive 状态时,如何使用启动画面隐藏应用内容
How to hide the app content with splash screen when app is in backgorund/inactive in flutter with package or method channel
问:
嘿,伙计们,我正在开发一个应用程序,当应用程序处于后台模式时,客户端希望在应用程序处于后台模式时显示启动画面,而不是使用标志安全的黑屏,任何可以帮助我处理 android 和 ios 的人 建议一个可以帮助我理解它或帮助我使用方法频道的包
这是两者的代码
人造人
class MainActivity: FlutterFragmentActivity() {
private val CHANNEL = "security"
override fun onCreate(savedInstanceState: Bundle?) {
if (intent.getIntExtra("org.chromium.chrome.extra.TASK_ID", -1) == this.taskId) {
this.finish()
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
super.onCreate(savedInstanceState)
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
setupMethodChannel(flutterEngine)
}
private fun setupMethodChannel(flutterEngine: FlutterEngine) {
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
when (call.method) {
"enableAppSecurity" -> {
enableAppSecurity()
result.success(null)
}
"disableAppSecurity" -> {
disableAppSecurity()
result.success(null)
}
else -> result.notImplemented()
}
}
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
toggleAppSecurity(hasFocus)
}
override fun onPause() {
super.onPause()
enableAppSecurity()
}
override fun onResume() {
super.onResume()
disableAppSecurity()
}
private fun toggleAppSecurity(hasFocus: Boolean) {
if (hasFocus) {
disableAppSecurity()
} else {
enableAppSecurity()
}
}
private fun enableAppSecurity() {
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
}
private fun disableAppSecurity() {
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
}
IOS系统
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
private var flutterViewController: FlutterViewController!
private var securityChannel: FlutterMethodChannel!
private var blurEffectView: UIVisualEffectView?
private var isInBackground: Bool = false // Track whether app is in background
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
setupFlutterCommunication()
// FirebaseApp.configure()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
private func setupFlutterCommunication() {
flutterViewController = window?.rootViewController as? FlutterViewController
securityChannel = FlutterMethodChannel(
name: "security",
binaryMessenger: flutterViewController.binaryMessenger
)
securityChannel.setMethodCallHandler(handle)
}
override func applicationDidEnterBackground(_ application: UIApplication) {
isInBackground = true // App entered background
enableAppSecurity()
}
override func applicationDidBecomeActive(_ application: UIApplication) {
// Check if the app was in background before becoming active
if isInBackground {
disableAppSecurity()
isInBackground = false
}
}
private func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "enableAppSecurity":
result(nil)
case "disableAppSecurity":
result(nil)
default:
result(FlutterMethodNotImplemented)
}
}
private func enableAppSecurity() {
let blurEffect = UIBlurEffect(style: .light)
blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView?.frame = window!.frame
window?.addSubview(blurEffectView!)
}
private func disableAppSecurity() {
blurEffectView?.removeFromSuperview()
}
}
答: 暂无答案
评论