Is it possible to get application's context in an Android Library Project?

后端 未结 5 1432
有刺的猬
有刺的猬 2020-12-04 18:53

I would like to get the context of application which has reference/hosted my library at run-time inside one class of my library project. Is it possible? If yes, how?

5条回答
  •  春和景丽
    2020-12-04 19:22

    There's another way to get context in jni, neither passing a parameter nor saving context by yourself, but by using android api. I found that there's a class named:

    • android.app.AppGlobals

    in the source code. And the static function

    • getInitialApplication

      can return an Application object. But it must be called in main thread, and the class is hidden. Anyway you can use it by reflecting in java. And you can just use FindClass() and FindStaticObjectMethod() to find out the method, and use it. Hope that helps.

提交回复
热议问题