Created
December 3, 2014 22:22
-
-
Save alain75007/64fcd7b01bef3126d316 to your computer and use it in GitHub Desktop.
This class is a singleton class which initializes core objects of Android Volley library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myschool.simpleapp; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.util.LruCache; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.toolbox.ImageLoader; | |
import com.android.volley.toolbox.Volley; | |
/** | |
* Created by alain on 02/12/14. | |
*/ | |
public class MySingleton { | |
private static MySingleton mInstance; | |
private RequestQueue mRequestQueue; | |
private final ImageLoader mImageLoader; | |
private static Context mCtx; | |
private MySingleton(Context context) { | |
mCtx = context; | |
mRequestQueue = getRequestQueue(); | |
mImageLoader = new ImageLoader(mRequestQueue, | |
new ImageLoader.ImageCache() { | |
private final LruCache<String, Bitmap> | |
cache = new LruCache<String, Bitmap>(20); | |
@Override | |
public Bitmap getBitmap(String url) { | |
return cache.get(url); | |
} | |
@Override | |
public void putBitmap(String url, Bitmap bitmap) { | |
cache.put(url, bitmap); | |
} | |
}); | |
} | |
public static synchronized MySingleton getInstance(Context context) { | |
if (mInstance == null) { | |
mInstance = new MySingleton(context); | |
} | |
return mInstance; | |
} | |
public RequestQueue getRequestQueue() { | |
if (mRequestQueue == null) { | |
// getApplicationContext() is key, it keeps you from leaking the | |
// Activity or BroadcastReceiver if someone passes one in. | |
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); | |
} | |
return mRequestQueue; | |
} | |
public <T> void addToRequestQueue(Request<T> req, String tag) { | |
getRequestQueue().add(req); | |
} | |
public <T> void addToRequestQueue(Request<T> req) { | |
getRequestQueue().add(req); | |
} | |
public ImageLoader getImageLoader() { | |
return mImageLoader; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment