How to get the Context within the Android library

假如想象 提交于 2020-06-17 15:49:10

问题


Sorry if my title did not match to what my questions is. I have created a Android Library, in which I have a Room database, As there should be only one instance of Room database, I have OfflineDatabaseManager getInstance method which provides the instance to the Android project which accesses it by passing the context. I have context within the Android project and I can pass it.

I want to listen to changes happening on the database table within the library so I can do something with it, I have written a class OfflineDataChangeListener within the library but to get the instance of the database I need to pass the context, how can I do that within the library please.

Library - OfflineDatabaseManager

class OfflineDatabaseManager private constructor(private val dp: LibraryDatabase) {

    fun getOfflineData() : Flow<List<OfflineData>> {
        return dp.getOfflineDataDao().getOfflineData()
    }

    suspend fun insertOfflineData(offlineData: OfflineData) {
        dp.getOfflineDataDao().insertOfflineData(offlineData)
    }

    companion object {
        @Volatile
        private var INSTANCE: OfflineDatabaseManager? = null

        fun getInstance(context: Context): OfflineDatabaseManager {
            return INSTANCE ?: synchronized(this) {
                INSTANCE ?: run {
                    val db = Room.databaseBuilder(
                        context,
                        LibraryDatabase::class.java, "database-name"
                    ).build()
                    OfflineDatabaseManager(db).also { INSTANCE = it }
                }
            }
        }
    }
}

Library - OfflineDataChangeListener - HOW CAN I PASS CONTEXT TO GET THE INSTANCE OF DB

class OfflineDataChangeListener: CoroutineScope {

    private var job: Job = Job()

    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job

    fun observeOfflineDataChanges() {
        launch {
            OfflineDatabaseManager.getInstance(HOW TO GET CONTEXT HERE).getOfflineData().collect {
                Log.d("dbChangeListener", "I am listening to databas echanges")
            }
        }
    }
}

Android project -

Within my android project this is how I access and listen to changes

 fun getOfflineData() {
            launch {
                OfflineDatabaseManager.getInstance(app.applicationContext).getOfflineData().collect {
                    Timber.d( "observing offline data" + it.toString())
                }
            }
        }

I want to do the same but within the library.

Thanks R


回答1:


You can provide some function to the clients which passes the Context to your library. In your library create an object class:

object Library {
    lateinit var context: Context
    fun init(ctx: Context) {
        context = ctx.applicationContext
    }
}

Clients must call your init() function, for example, in their Application's onCreate() method:

Library.init(this)

And in the library you can access to it like this:

OfflineDatabaseManager.getInstance(Library.context)


来源:https://stackoverflow.com/questions/62234384/how-to-get-the-context-within-the-android-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!