BackgroundTask.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package de.tudarmstadt.informatik.hostage.sync.wifi_direct;
  2. import android.os.AsyncTask;
  3. /**
  4. * Created by Julien on 07.01.2015.
  5. */
  6. public class BackgroundTask extends AsyncTask<Void, Void, String> {
  7. /**
  8. * This listener calls didSucceed if the method performInBackground will return true, otherwise it calls didFail.
  9. */
  10. public interface BackgroundTaskCompletionListener {
  11. public void didSucceed();
  12. public void didFail();
  13. }
  14. private boolean isInterrupted;
  15. private boolean wasSuccessfully;
  16. public void interrupt(boolean b){
  17. this.isInterrupted = b;
  18. }
  19. public boolean isInterrupted() {
  20. return isInterrupted;
  21. }
  22. private BackgroundTaskCompletionListener completion_listener;
  23. public BackgroundTask(BackgroundTaskCompletionListener l){
  24. super();
  25. this.completion_listener = l;
  26. }
  27. /**
  28. * Interrupts the background process.
  29. * @param isInterrupted
  30. */
  31. public void setInterrupted(boolean isInterrupted) {
  32. this.isInterrupted = isInterrupted;
  33. }
  34. /**
  35. * Do any stuff here if it should run in the background. It should return true or false if the process was successfully.
  36. * @return boolean
  37. */
  38. public boolean performInBackground(){
  39. return true;
  40. }
  41. @Override
  42. protected String doInBackground(Void... params) {
  43. this.wasSuccessfully = this.performInBackground();
  44. return null;
  45. }
  46. /*
  47. * (non-Javadoc)
  48. * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
  49. */
  50. @Override
  51. protected void onPostExecute(String result) {
  52. if (this.completion_listener != null) {
  53. if (this.wasSuccessfully) {
  54. this.completion_listener.didSucceed();
  55. }else {
  56. this.completion_listener.didFail();
  57. }
  58. }
  59. }
  60. /*
  61. * (non-Javadoc)
  62. * @see android.os.AsyncTask#onPreExecute()
  63. */
  64. @Override
  65. protected void onPreExecute() {
  66. }
  67. }