17 changed files with 534 additions and 247 deletions
@ -0,0 +1,144 @@ |
|||||
|
package com.saitong.voitrans |
||||
|
|
||||
|
import android.app.ActivityManager |
||||
|
import android.content.Context |
||||
|
import android.util.Log |
||||
|
import io.flutter.app.FlutterApplication |
||||
|
import io.flutter.embedding.engine.FlutterEngine |
||||
|
import io.flutter.embedding.engine.FlutterEngineCache |
||||
|
import io.flutter.embedding.engine.dart.DartExecutor |
||||
|
import io.flutter.plugins.GeneratedPluginRegistrant |
||||
|
|
||||
|
// Application.kt |
||||
|
class MyApp : FlutterApplication() { |
||||
|
|
||||
|
companion object { |
||||
|
private const val TAG = "MyApp" |
||||
|
private const val ENGINE_ID = "main_engine" |
||||
|
|
||||
|
// 标记 Engine 是否已经执行过 Dart 入口点 |
||||
|
@Volatile |
||||
|
private var isDartEntrypointExecuted = false |
||||
|
|
||||
|
// 记录应用启动时间,用于判断是否为新启动 |
||||
|
@Volatile |
||||
|
private var appStartTime = 0L |
||||
|
} |
||||
|
|
||||
|
override fun onCreate() { |
||||
|
super.onCreate() |
||||
|
appStartTime = System.currentTimeMillis() |
||||
|
Log.d(TAG, "MyApp onCreate - 进程启动") |
||||
|
|
||||
|
val cache = FlutterEngineCache.getInstance() |
||||
|
val existingEngine = cache.get(ENGINE_ID) |
||||
|
|
||||
|
if (existingEngine == null) { |
||||
|
Log.d(TAG, "创建新的 FlutterEngine") |
||||
|
createAndCacheEngine(cache) |
||||
|
} else { |
||||
|
Log.d(TAG, "FlutterEngine 已存在,检查状态") |
||||
|
// 检查现有 Engine 的状态 |
||||
|
try { |
||||
|
val isExecuting = existingEngine.dartExecutor.isExecutingDart |
||||
|
Log.d(TAG, "Engine Dart执行状态: $isExecuting") |
||||
|
|
||||
|
if (!isExecuting && isMainProcess()) { |
||||
|
Log.d(TAG, "Engine 存在但Dart未执行,重新执行") |
||||
|
existingEngine.dartExecutor.executeDartEntrypoint( |
||||
|
DartExecutor.DartEntrypoint.createDefault() |
||||
|
) |
||||
|
isDartEntrypointExecuted = true |
||||
|
} |
||||
|
} catch (e: Exception) { |
||||
|
Log.e(TAG, "检查或重新执行 Dart 入口点失败", e) |
||||
|
// 如果失败,重新创建 Engine |
||||
|
cache.remove(ENGINE_ID) |
||||
|
createAndCacheEngine(cache) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private fun createAndCacheEngine(cache: FlutterEngineCache) { |
||||
|
// 在主进程中总是创建并执行Dart入口点 |
||||
|
if (isMainProcess()) { |
||||
|
Log.d(TAG, "✅ 在主进程创建 FlutterEngine 并执行 Dart") |
||||
|
val flutterEngine = FlutterEngine(this) |
||||
|
|
||||
|
// 注册插件(只在这里注册一次) |
||||
|
Log.d(TAG, "📦 注册 Flutter 插件") |
||||
|
GeneratedPluginRegistrant.registerWith(flutterEngine) |
||||
|
|
||||
|
try { |
||||
|
Log.d(TAG, "🚀 开始执行 Dart 入口点") |
||||
|
flutterEngine.dartExecutor.executeDartEntrypoint( |
||||
|
DartExecutor.DartEntrypoint.createDefault() |
||||
|
) |
||||
|
isDartEntrypointExecuted = true |
||||
|
cache.put(ENGINE_ID, flutterEngine) |
||||
|
Log.d(TAG, "✅ FlutterEngine 创建并缓存成功") |
||||
|
} catch (e: Exception) { |
||||
|
Log.e(TAG, "❌ 执行 Dart 入口点失败", e) |
||||
|
// 如果执行失败,仍然缓存 Engine,但标记为未执行 |
||||
|
isDartEntrypointExecuted = false |
||||
|
cache.put(ENGINE_ID, flutterEngine) |
||||
|
} |
||||
|
} else { |
||||
|
Log.d(TAG, "⚠️ 在非主进程,仅创建 FlutterEngine") |
||||
|
// 非主进程只创建 Engine,不执行 Dart 入口点 |
||||
|
val flutterEngine = FlutterEngine(this) |
||||
|
cache.put(ENGINE_ID, flutterEngine) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private fun isMainProcess(): Boolean { |
||||
|
val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager |
||||
|
val processes = am.runningAppProcesses ?: return true |
||||
|
val mainProcessName = packageName |
||||
|
val myPid = android.os.Process.myPid() |
||||
|
|
||||
|
for (process in processes) { |
||||
|
if (process.pid == myPid) { |
||||
|
return process.processName == mainProcessName |
||||
|
} |
||||
|
} |
||||
|
return true |
||||
|
} |
||||
|
|
||||
|
private fun isAppInForeground(): Boolean { |
||||
|
val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager |
||||
|
val processes = am.runningAppProcesses ?: return false |
||||
|
val pkg = packageName |
||||
|
for (p in processes) { |
||||
|
if (p.processName == pkg && |
||||
|
p.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { |
||||
|
return true |
||||
|
} |
||||
|
} |
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
private fun isAppBeingLaunched(): Boolean { |
||||
|
// 改进的启动检测逻辑 |
||||
|
val currentTime = System.currentTimeMillis() |
||||
|
val isRecentStart = (currentTime - appStartTime) < 3000 // 3秒内认为是新启动 |
||||
|
|
||||
|
if (isRecentStart) { |
||||
|
Log.d(TAG, "检测到应用新启动") |
||||
|
return true |
||||
|
} |
||||
|
|
||||
|
// 备用检测方法(兼容旧版本) |
||||
|
return try { |
||||
|
val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager |
||||
|
val tasks = am.getRunningTasks(1) |
||||
|
val result = tasks.isNotEmpty() && tasks[0].topActivity?.packageName == packageName |
||||
|
Log.d(TAG, "通过 RunningTasks 检测启动状态: $result") |
||||
|
result |
||||
|
} catch (e: Exception) { |
||||
|
Log.w(TAG, "无法获取 RunningTasks,可能缺少权限", e) |
||||
|
// 如果无法获取任务信息,默认认为是启动状态 |
||||
|
true |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue