VolleySingleton.kt 910 B

123456789101112131415161718192021222324252627282930
  1. package com.app.demoe
  2. import android.content.Context
  3. import com.android.volley.Request
  4. import com.android.volley.RequestQueue
  5. import com.android.volley.toolbox.Volley
  6. class VolleySingleton constructor(context: Context) {
  7. companion object {
  8. @Volatile
  9. private var INSTANCE: VolleySingleton? = null
  10. fun getInstance(context: Context) =
  11. INSTANCE ?: synchronized(this) {
  12. INSTANCE ?: VolleySingleton(context).also {
  13. INSTANCE = it
  14. }
  15. }
  16. }
  17. private val requestQueue: RequestQueue by lazy {
  18. // applicationContext is key, it keeps you from leaking the
  19. // Activity or BroadcastReceiver if someone passes one in.
  20. Volley.newRequestQueue(context.applicationContext)
  21. }
  22. fun <T> addToRequestQueue(req: Request<T>) {
  23. requestQueue.add(req)
  24. }
  25. }