Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.util.Log;
- 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;
- public class MySingleton {
- private static MySingleton mInstance;
- private Context mContext;
- private RequestQueue mResquestQueue;
- private ImageLoader mImageLoader;
- public static final String TAG = "request_tag";
- private MySingleton(Context mCtx){
- this.mContext = mCtx;
- mResquestQueue = Volley.newRequestQueue(AidMaidApplication.getAppContext());
- mImageLoader = new ImageLoader(mResquestQueue , new ImageLoader.ImageCache(){
- private final LruCache<String , Bitmap> cache = new LruCache<>(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 getResquestQueue(){
- if(mResquestQueue == null){
- mResquestQueue= Volley.newRequestQueue(mContext.getApplicationContext());
- }
- return mResquestQueue;
- }
- public <T> void addToRequestQueue(Request<T> request){
- request.setTag(TAG);
- mResquestQueue.add(request);
- }
- public ImageLoader getmImageLoader(){
- return mImageLoader;
- }
- public void CancelAllRequests(){
- Log.i(TAG, "Cancel All Requests");
- if(mResquestQueue != null){
- mResquestQueue.cancelAll(TAG);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement