Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. private List<CustomFile> taskList;
  2. private AsyncTask fileTask;
  3.  
  4. @Override
  5. public void onCreate() {
  6. super.onCreate();
  7. taskList = new ArrayList<>();
  8. fileTask = new fileTaskAsync();
  9. }
  10.  
  11. @Override
  12. public int onStartCommand(Intent intent, int flags, int startId) {
  13.  
  14. String filePath = intent.getStringExtra("filePath");
  15. String fileType = intent.getStringExtra("fileType");
  16. String taskType = intent.getStringExtra("taskType");
  17. String fileName = intent.getStringExtra("fileName");
  18.  
  19. CustomFile customFile = new CustomFile();
  20. customFile.filePath = filePath;
  21. customFile.fileType = fileType;
  22. customFile.taskType = taskType;
  23. customFile.fileName = fileName;
  24. taskList.add(customFile);
  25.  
  26. Notification notification = getNotification();
  27.  
  28. startForeground(787, notification);
  29.  
  30. if (fileTask.getStatus() != AsyncTask.Status.RUNNING) {
  31.  
  32. CustomFile current = taskList.get(0);
  33. taskList.remove(current);
  34.  
  35. fileTask = new fileTaskAsync().execute(current);
  36. }
  37.  
  38. stopSelf();
  39. return START_NOT_STICKY;
  40. }
  41.  
  42. @Nullable
  43. @Override
  44. public IBinder onBind(Intent intent) {
  45. return null;
  46. }
  47.  
  48. private class fileTaskAsync extends AsyncTask<CustomFile, Void, String> {
  49.  
  50. @Override
  51. protected String doInBackground(CustomFile... customFiles) {
  52.  
  53. CustomFile customFile = customFiles[0];
  54.  
  55. FileUtils.doFileTask(customFile.filePath, customFile.fileType,
  56. customFile.taskType);
  57.  
  58. return customFile.fileName;
  59. }
  60.  
  61. @Override
  62. protected void onPostExecute(String name) {
  63. sendResult(name);
  64.  
  65. if (!taskList.isEmpty()) {
  66. CustomFile newCurrent = taskList.get(0);
  67. taskList.remove(newCurrent);
  68. fileTask = new fileTaskAsync().execute(newCurrent);
  69. }
  70. }
  71. }
  72.  
  73. private void sendResult(String name) {
  74. Intent intent = new Intent("taskStatus");
  75. intent.putExtra("taskName", name);
  76. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement