从 ViewModel 使用 Volley Singleton
Use Volley Singleton from ViewModel
我有一个新的 Fragment,它的 ViewModel 是由 Android Studio 3.2.1 创建的。
objective是使用Googlehere提出的Volley Singleton发起请求。
问题是 Volley Singleton 的 Google Singleton 模式依赖于 ViewModel 中不直接存在的上下文。
如何从 ViewModel 获取应用程序上下文,然后由 Volley Singleton 使用?
WebsiteRestApi Singleton with Volley
package com.developer.pochttp.sampledata
import android.content.Context
import android.graphics.Bitmap
import android.support.v4.util.LruCache
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.ImageLoader
import com.android.volley.toolbox.Volley
class WebsiteRestApi constructor(context: Context) {
companion object {
@Volatile
private var INSTANCE: WebsiteRestApi? = null
fun getInstance(context: Context) =
INSTANCE ?: synchronized(this) {
INSTANCE ?: WebsiteRestApi(context).also {
INSTANCE = it
}
}
}
val imageLoader: ImageLoader by lazy {
ImageLoader(requestQueue,
object : ImageLoader.ImageCache {
private val cache = LruCache<String, Bitmap>(20)
override fun getBitmap(url: String): Bitmap? {
return cache.get(url)
}
override fun putBitmap(url: String, bitmap: Bitmap) {
cache.put(url, bitmap)
}
})
}
val requestQueue: RequestQueue by lazy {
// applicationContext is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
Volley.newRequestQueue(context.applicationContext)
}
fun <T> addToRequestQueue(req: Request<T>) {
requestQueue.add(req)
}
}
ViewModel
package com.developer.pochttp.ui.main
import android.arch.lifecycle.ViewModel
class MainViewModel : ViewModel() {
}
只需申请 class 就像:
public class MyApplication extends Application {
private static MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized MyApplication getInstance() {
return mInstance;
}
public static Context getAppContext() {
return mInstance.getApplicationContext();
}
}
Manifest.xml 文件编辑
<application
android:name=".MyApplication"
></application>
您可以为 volley 提供一个足以让 volley 运行的应用程序上下文。
所以现在
VolleySingleton.getInstance(MyApplication.getAppContext());
可以让你的截击从任何地方发挥作用。
我有一个新的 Fragment,它的 ViewModel 是由 Android Studio 3.2.1 创建的。
objective是使用Googlehere提出的Volley Singleton发起请求。
问题是 Volley Singleton 的 Google Singleton 模式依赖于 ViewModel 中不直接存在的上下文。
如何从 ViewModel 获取应用程序上下文,然后由 Volley Singleton 使用?
WebsiteRestApi Singleton with Volley
package com.developer.pochttp.sampledata
import android.content.Context
import android.graphics.Bitmap
import android.support.v4.util.LruCache
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.ImageLoader
import com.android.volley.toolbox.Volley
class WebsiteRestApi constructor(context: Context) {
companion object {
@Volatile
private var INSTANCE: WebsiteRestApi? = null
fun getInstance(context: Context) =
INSTANCE ?: synchronized(this) {
INSTANCE ?: WebsiteRestApi(context).also {
INSTANCE = it
}
}
}
val imageLoader: ImageLoader by lazy {
ImageLoader(requestQueue,
object : ImageLoader.ImageCache {
private val cache = LruCache<String, Bitmap>(20)
override fun getBitmap(url: String): Bitmap? {
return cache.get(url)
}
override fun putBitmap(url: String, bitmap: Bitmap) {
cache.put(url, bitmap)
}
})
}
val requestQueue: RequestQueue by lazy {
// applicationContext is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
Volley.newRequestQueue(context.applicationContext)
}
fun <T> addToRequestQueue(req: Request<T>) {
requestQueue.add(req)
}
}
ViewModel
package com.developer.pochttp.ui.main
import android.arch.lifecycle.ViewModel
class MainViewModel : ViewModel() {
}
只需申请 class 就像:
public class MyApplication extends Application {
private static MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized MyApplication getInstance() {
return mInstance;
}
public static Context getAppContext() {
return mInstance.getApplicationContext();
}
}
Manifest.xml 文件编辑
<application
android:name=".MyApplication"
></application>
您可以为 volley 提供一个足以让 volley 运行的应用程序上下文。 所以现在
VolleySingleton.getInstance(MyApplication.getAppContext());
可以让你的截击从任何地方发挥作用。