Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private List<CustomFile> taskList;
- private AsyncTask fileTask;
- @Override
- public void onCreate() {
- super.onCreate();
- taskList = new ArrayList<>();
- fileTask = new fileTaskAsync();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- String filePath = intent.getStringExtra("filePath");
- String fileType = intent.getStringExtra("fileType");
- String taskType = intent.getStringExtra("taskType");
- String fileName = intent.getStringExtra("fileName");
- CustomFile customFile = new CustomFile();
- customFile.filePath = filePath;
- customFile.fileType = fileType;
- customFile.taskType = taskType;
- customFile.fileName = fileName;
- taskList.add(customFile);
- Notification notification = getNotification();
- startForeground(787, notification);
- if (fileTask.getStatus() != AsyncTask.Status.RUNNING) {
- CustomFile current = taskList.get(0);
- taskList.remove(current);
- fileTask = new fileTaskAsync().execute(current);
- }
- stopSelf();
- return START_NOT_STICKY;
- }
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- private class fileTaskAsync extends AsyncTask<CustomFile, Void, String> {
- @Override
- protected String doInBackground(CustomFile... customFiles) {
- CustomFile customFile = customFiles[0];
- FileUtils.doFileTask(customFile.filePath, customFile.fileType,
- customFile.taskType);
- return customFile.fileName;
- }
- @Override
- protected void onPostExecute(String name) {
- sendResult(name);
- if (!taskList.isEmpty()) {
- CustomFile newCurrent = taskList.get(0);
- taskList.remove(newCurrent);
- fileTask = new fileTaskAsync().execute(newCurrent);
- }
- }
- }
- private void sendResult(String name) {
- Intent intent = new Intent("taskStatus");
- intent.putExtra("taskName", name);
- LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement