You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MySingleton.java 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package com.example.fcm;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import com.android.volley.Request;
  5. import com.android.volley.RequestQueue;
  6. import com.android.volley.toolbox.Volley;
  7. public class MySingleton {
  8. @SuppressLint("StaticFieldLeak")
  9. private static MySingleton instance;
  10. private RequestQueue requestQueue;
  11. private Context ctx;
  12. private MySingleton(Context context) {
  13. ctx = context;
  14. requestQueue = getRequestQueue();
  15. }
  16. public static synchronized MySingleton getInstance(Context context) {
  17. if (instance == null) {
  18. instance = new MySingleton(context);
  19. }
  20. return instance;
  21. }
  22. public RequestQueue getRequestQueue() {
  23. if (requestQueue == null) {
  24. // getApplicationContext() is key, it keeps you from leaking the
  25. // Activity or BroadcastReceiver if someone passes one in.
  26. requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
  27. }
  28. return requestQueue;
  29. }
  30. public <T> void addToRequestQueue(Request<T> req) {
  31. getRequestQueue().add(req);
  32. }
  33. }