Needing Context in non-Activity classes

前端 未结 7 1732
轻奢々
轻奢々 2020-12-14 06:10

I have some classes within my application that need to call Android functions that require the Context as a parameter. I don\'t have it as the class is not a subclass of the

7条回答
  •  一个人的身影
    2020-12-14 06:39

    I've answered this question here also.

    You can do that using ContextWrapper, as described here.

    For example:

    public class MyContextWrapper extends ContextWrapper {
    
        public MyContextWrapper(Context base) {
          super(base);
       }
    
    }
    

    and use that class as it were Context

提交回复
热议问题