1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package com.example.fcm;
-
- import android.annotation.SuppressLint;
- import android.content.Context;
-
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.toolbox.Volley;
-
- public class MySingleton {
- @SuppressLint("StaticFieldLeak")
- private static MySingleton instance;
- private RequestQueue requestQueue;
- private Context ctx;
-
- private MySingleton(Context context) {
- ctx = context;
- requestQueue = getRequestQueue();
- }
-
- public static synchronized MySingleton getInstance(Context context) {
- if (instance == null) {
- instance = new MySingleton(context);
- }
- return instance;
- }
-
- public RequestQueue getRequestQueue() {
- if (requestQueue == null) {
- // getApplicationContext() is key, it keeps you from leaking the
- // Activity or BroadcastReceiver if someone passes one in.
- requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
- }
- return requestQueue;
- }
-
- public <T> void addToRequestQueue(Request<T> req) {
- getRequestQueue().add(req);
- }
-
- }
|