【问题标题】:How to access Android module application context如何访问 Android 模块应用程序上下文
【发布时间】:2018-10-12 07:46:42
【问题描述】:
我创建了一个有助于访问 HTTP 调用(Http Module wrap volley)的 android 模块(模块项目)。我想做 Volley.newRequestQueue(mContext);到一个初始化一次而不是每次都创建的地方(以避免内存溢出)。一个更好的地方是应用程序类,但从模块中,我不想访问应用程序。有没有什么地方可以初始化一次 volley requestQue 然后使用它。模块中是否有类似应用程序的组件?
【问题讨论】:
标签:
android
android-volley
android-module
【解决方案1】:
我确实在我的模块中创建了一个单例类来获取请求
public class RequestQueSingleton {
private static RequestQueSingleton sSoleInstance;
private static RequestQueue reQuestQue;
private RequestQueSingleton(){} //private constructor.
public static RequestQueSingleton getInstance(Context context){
if (sSoleInstance == null){ //if there is no instance available... create new one
sSoleInstance = new RequestQueSingleton();
reQuestQue = Volley.newRequestQueue(context);
}
return sSoleInstance;
}
public synchronized RequestQueue getInstance() {
Log.d("Request Que Obj",reQuestQue.hashCode()+"");
return reQuestQue;
}
}