在另一个模块中使用应用程序上下文

问题描述 投票:0回答:3

我有一个像 FileLog 这样的单例类,我想在其方法之一中创建一个文件,并且需要一个 Context 来执行此操作。我的问题是 FileLog 类被放置在另一个模块中,并且我无权访问应用程序模块上下文。你能告诉我如何使用 Hilt 或其他方式访问上下文吗?.

已编辑-------------------------->>>>><<<<<<--------------------------

这是我的单例类。如果我向它传递一个上下文,我将面临内存泄漏。我仅在一种方法中需要上下文,并且不想将上下文作为参数传递给该方法。如果我访问一个扩展 Application() 的类并且其中有一个静态上下文,我可以在 FileLog 类方法中使用它。

open class FileLog private constructor(context: Context) {
private lateinit var streamWriter: OutputStreamWriter
private var dateFormat: SimpleDateFormat? = null
private var currentFile: File? = null
private var networkFile: File? = null
private var tonlibFile: File? = null
private var initied = false
private var context: Context = context

companion object {
    lateinit var INSTANCE: FileLog

    @Synchronized
    fun getInstance(context: Context): FileLog {
        if (!Companion::INSTANCE.isInitialized) {
            INSTANCE = FileLog(context)
        }

        return INSTANCE
    }

 }
 fun initFileLog(context: Context) {
    Log.e("FileLog", "create file")

    if (initied) {
        return
    }
    dateFormat = SimpleDateFormat("dd_MM_yyyy_HH_mm_ss", Locale.US)
    try {
        val sdCard: File = context.getExternalFilesDir(null)
            ?: return
        val dir = File(sdCard.absolutePath + "/logs")
        dir.mkdirs()
        currentFile =
            File(dir, dateFormat?.format(System.currentTimeMillis()).toString() + ".txt")
    } catch (e: Exception) {
        e.printStackTrace()
    }
 }   
android android-context dagger-hilt modular
3个回答
3
投票

您的记录器真的需要

Context
吗?不,它需要
File
外部文件目录。

fun initFileLog(sdCard: File) {
    Log.e("FileLog", "create file")

    if (initied) {
        return
    }
    dateFormat = SimpleDateFormat("dd_MM_yyyy_HH_mm_ss", Locale.US)
    try {
        val dir = File(sdCard.absolutePath + "/logs")
    ...
    }
}}

您可以从应用程序模块提供这样的

File

根据经验,您应该避免模块中的 Android 依赖项,这样可以在更简单的环境中测试它们。


0
投票

您可以在 App 类中公开应用程序上下文,而不是将上下文传递给方法:

class App : Application() {
    companion object {
        val context: App
            get() = contextReference.get()!! as App
    }
}

并像这样使用它:

App.context.getExternalFilesDir(null)

0
投票

我认为你也可以使用对象类,因为它只包含一个实例

object FileLog{
private lateinit var streamWriter: OutputStreamWriter
private var dateFormat: SimpleDateFormat? = null
private var currentFile: File? = null
private var networkFile: File? = null
private var tonlibFile: File? = null
private var initied = false

fun initFileLog(context: Context) {
    Log.e("FileLog", "create file")

    if (initied) {
        return
    }
    dateFormat = SimpleDateFormat("dd_MM_yyyy_HH_mm_ss", Locale.US)
    try {
        val sdCard: File = context.getExternalFilesDir(null)
            ?: return
        val dir = File(sdCard.absolutePath + "/logs")
        dir.mkdirs()
        currentFile =
            File(dir, dateFormat?.format(System.currentTimeMillis()).toString() + ".txt")
    } catch (e: Exception) {
        e.printStackTrace()
    }
}}
© www.soinside.com 2019 - 2024. All rights reserved.