Advertisement
minafaw3

MySingleton

Feb 1st, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1.  
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.util.Log;
  6. import android.util.LruCache;
  7.  
  8. import com.android.volley.Request;
  9. import com.android.volley.RequestQueue;
  10. import com.android.volley.toolbox.ImageLoader;
  11. import com.android.volley.toolbox.Volley;
  12.  
  13. public class MySingleton {
  14.  
  15. private static MySingleton mInstance;
  16. private Context mContext;
  17. private RequestQueue mResquestQueue;
  18. private ImageLoader mImageLoader;
  19. public static final String TAG = "request_tag";
  20.  
  21. private MySingleton(Context mCtx){
  22. this.mContext = mCtx;
  23. mResquestQueue = Volley.newRequestQueue(AidMaidApplication.getAppContext());
  24. mImageLoader = new ImageLoader(mResquestQueue , new ImageLoader.ImageCache(){
  25. private final LruCache<String , Bitmap> cache = new LruCache<>(20);
  26.  
  27. @Override
  28. public Bitmap getBitmap(String url) {
  29. return cache.get(url);
  30. }
  31.  
  32. @Override
  33. public void putBitmap(String url, Bitmap bitmap) {
  34. cache.put(url , bitmap);
  35. }
  36. });
  37. }
  38. public static synchronized MySingleton getInstance(Context context){
  39. if(mInstance == null){
  40. mInstance = new MySingleton(context);
  41. }
  42. return mInstance;
  43. }
  44. public RequestQueue getResquestQueue(){
  45. if(mResquestQueue == null){
  46. mResquestQueue= Volley.newRequestQueue(mContext.getApplicationContext());
  47. }
  48. return mResquestQueue;
  49. }
  50.  
  51. public <T> void addToRequestQueue(Request<T> request){
  52. request.setTag(TAG);
  53. mResquestQueue.add(request);
  54. }
  55.  
  56. public ImageLoader getmImageLoader(){
  57. return mImageLoader;
  58. }
  59.  
  60. public void CancelAllRequests(){
  61. Log.i(TAG, "Cancel All Requests");
  62. if(mResquestQueue != null){
  63. mResquestQueue.cancelAll(TAG);
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement