Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. package admin4.techelm.com.techelmtechnologies.task;
  2.  
  3. import android.app.Activity;
  4. import android.os.AsyncTask;
  5. import android.os.Handler;
  6.  
  7. /**
  8. * Created by admin 4 on 31/03/2017.
  9. * To cancel the task if the loading is running on a certain amount of time
  10. * And when you call your AsyncTask, you need to run the cancle task after a certain amount of time (=the timeout, in this case 20 sec)
  11.  
  12. private Handler handler = new Handler();
  13. private TaskCanceler taskCanceler;
  14. ...
  15. LoadData task = new LoadData();
  16. taskCanceler = new TaskCanceler(task);
  17. handler.postDelayed(taskCanceler, 20*1000);
  18. task.execute(...)
  19. It's a good idea if you clean this up on cancel or finish with
  20.  
  21. if(taskCanceler != null && handler != null) {
  22. handler.removeCallbacks(taskCanceler);
  23. }
  24. You can of course wrap this in an custom implementation of AsyncTask.
  25. I've used this pattern many times and it works like a charm.
  26. One thing to note, in rare cases the handler would not start,
  27. I suspect if you create it in the wrong context it will not survive in certain instances,
  28. so I forced the handler to be an the UI Thread with
  29. handler= new Handler(Looper.getMainLooper());
  30. */
  31.  
  32. /** @VERSION2
  33. * You can use this on Activity as follow
  34.  
  35. yourTask = new SJTask_RenderList(formattedDate, "", mContext);
  36. new TaskCanceller(yourTask).setWait(getActivity());
  37. yourTask.execute((Void) null)
  38.  
  39. * In version 1 you need to call this.mHandler.removeCallbacks(this.mTaskCanceller);,
  40. * then in Version 2 don't need to do anuthing since removeCallbacks is Automatically Handled by the run() method
  41. * And then you call the Override onCalled in you Asynctask Class, like so; for your own action
  42.  
  43. @Override
  44. protected void onCancelled() {
  45. Log.i(TAG, "onCancelled hideSwipeRefreshing() new SJTask_RenderList()");
  46. }
  47.  
  48. */
  49. public class TaskCanceller implements Runnable {
  50.  
  51. private final static long LONG_DELAY = 20000; // Define how long the default delay will be
  52. private long taskDelay = 5*1000; // 2 Seconds if not connected to internet
  53.  
  54. private AsyncTask task;
  55. private Handler mHandler = null;
  56. private TaskCanceller mTaskCanceller;
  57.  
  58. public TaskCanceller(AsyncTask task) {
  59. this.mHandler = new Handler();
  60. this.task = task;
  61. this.mTaskCanceller = this;
  62. }
  63.  
  64. public TaskCanceller setWait(Activity activity) {
  65. taskDelay = LONG_DELAY;
  66. this.mHandler.postDelayed(mTaskCanceller, taskDelay); // 20*1000 = 20 Seconds,
  67. return this;
  68. }
  69.  
  70. // Use this to Customize the Leng of Delay in Milliseconds
  71. public void setLongDelay(long delay) {
  72. this.taskDelay = delay;
  73. }
  74.  
  75. @Override
  76. public void run() {
  77. if (this.task.getStatus() == AsyncTask.Status.RUNNING)
  78. this.task.cancel(true);
  79. else { // Task is running already and 20 Secs is still counting
  80. if (this.mTaskCanceller != null && this.mHandler != null) {
  81. this.mHandler.removeCallbacks(this.mTaskCanceller);
  82. }
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement