package com.shuwei.intelligent.shelves import android.app.Application import android.content.Context import android.content.Intent import android.content.IntentFilter import android.content.SharedPreferences import android.os.Handler import android.os.Looper import com.shuwei.intelligent.shelves.utils.BootReceiver import com.shuwei.intelligent.shelves.utils.CrashHandler import com.shuwei.intelligent.shelves.utils.FileLogger /** * @author: star * @date: 2022-04-26 */ class App : Application() { //"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjYW50ZWVuSWQiOiIiLCJ0eXBlIjoiNiIsInVzZXJJZCI6IjE2NzgyMjY1NTI5MzE1NDkxODYifQ.CQmbELpqT6QzvVCgCeirwvSvPp8Mhft_h6b1Af574Co" override fun onCreate() { super.onCreate() app = this CrashHandler.init(this) // 启动时立即清理一次过期日志 cleanupLogs() // 设备长期不断电、进程不会重新启动,运行中每24小时定期清理一次 logCleanupHandler.postDelayed(logCleanupRunnable, LOG_CLEANUP_INTERVAL) val filter = IntentFilter(Intent.ACTION_BOOT_COMPLETED) registerReceiver(BootReceiver(), filter) } /** * 清理过期的日志文件:文件日志与崩溃日志均保留最近7天 */ private fun cleanupLogs() { FileLogger.cleanupExpiredLogs(this) CrashHandler.getInstance()?.cleanupOldCrashReports() } companion object { private lateinit var app: App var canteenId = "0" var appVersion = "1" var configUrl = "" //从接口获取 var accessToken: String = "" var deviceId: String = "" @Volatile private var sharedPref: SharedPreferences? = null /** 日志定期清理间隔:24小时,与日志文件按天滚动对齐 */ private const val LOG_CLEANUP_INTERVAL = 24L * 60 * 60 * 1000 private val logCleanupHandler = Handler(Looper.getMainLooper()) /** 日志定期清理任务:执行后重新调度自身,形成循环 */ private val logCleanupRunnable = object : Runnable { override fun run() { getInstance().cleanupLogs() logCleanupHandler.postDelayed(this, LOG_CLEANUP_INTERVAL) } } fun getInstance(): App { return app } fun getSharedPref(): SharedPreferences? { if (sharedPref != null) { return sharedPref } sharedPref = app.getSharedPreferences("sp_intelligent_shelves", Context.MODE_PRIVATE) return sharedPref } } }