Advertisement
ProgrameruPokusaju

andr

Sep 26th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package d_logic.ticketsale;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.AsyncTask;
  7. import android.widget.Toast;
  8.  
  9. import java.util.HashMap;
  10. import java.util.Map;
  11.  
  12. import d_logic.translator.Translator;
  13.  
  14. /**
  15.  * Author: Aleksandar Arsic
  16.  * Description:
  17.  * Date created:  9/26/17 (mm/dd/yy)
  18.  */
  19. public class AsyncTaskManager extends BroadcastReceiver {
  20.  
  21.     /**
  22.      * Fields
  23.      */
  24.     private static HashMap<String,AsyncTask> hashMap;
  25.  
  26.     /**
  27.      * Singleton pattern
  28.      */
  29.     private static AsyncTaskManager instance = null;
  30.     private AsyncTaskManager() {
  31.         hashMap = new HashMap<String, AsyncTask>();
  32.     }
  33.  
  34.     public AsyncTaskManager getInstance() {
  35.         if(instance == null) {
  36.             instance = new AsyncTaskManager();
  37.         }
  38.         return instance;
  39.     }
  40.  
  41.     /**
  42.      * Add new AsyncTask in HashMap so that application can keep track of active async tasks
  43.      * @param asyncTaskName
  44.      * @param asyncTask
  45.      */
  46.     public static void addAsyncTask(String asyncTaskName,AsyncTask asyncTask) {
  47.         hashMap.put(asyncTaskName,asyncTask);
  48.     }
  49.  
  50.     /**
  51.      * Remove specific async task from HashMap
  52.      * @param asyncTaskName
  53.      */
  54.     public static void removeAsyncTask(String asyncTaskName) {
  55.         hashMap.get(asyncTaskName).cancel(true);
  56.         hashMap.remove(asyncTaskName);
  57.     }
  58.  
  59.     /**
  60.      * Remove all asynctasks
  61.      */
  62.     public static void clearAll() {
  63.         for(Map.Entry m:hashMap.entrySet()){
  64.             removeAsyncTask(m.getKey().toString());
  65.         }
  66.     }
  67.  
  68.     @Override
  69.     public void onReceive(Context context, Intent intent) {      
  70.         if(Util.isNetworkAvailable(context)) {
  71.          
  72.         } else {
  73.            clearAll();
  74.            Intent noConnection = new Intent(context, NoConnection.class);
  75.            noConnection.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  76.            context.startActivity(noConnection);
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement