Advertisement
Guest User

my intent service

a guest
Mar 18th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. package com.gorsek.filip.services;
  2.  
  3. import android.app.IntentService;
  4. import android.content.Intent;
  5. import android.os.Handler;
  6. import android.widget.Toast;
  7.  
  8.  
  9. public class MyIntentService extends IntentService {
  10.  
  11. private Handler handler = new Handler();
  12.  
  13.  
  14. public MyIntentService() {
  15. super("MyIntentService");
  16. }
  17.  
  18. @Override
  19. protected void onHandleIntent(Intent intent) {
  20. if (intent != null) {
  21. int taskCount = intent.getIntExtra(MainActivity.TASK_COUNT, 0);
  22. for (int i = 0; i < taskCount; i++) {
  23. performLongTask();
  24. int progress = ((int) ((i + 1) / (double) taskCount * 100));
  25. showProgress(progress);
  26. }
  27. }
  28. }
  29.  
  30. private void showProgress(final int progress) {
  31. handler.post(new Runnable() {
  32. @Override
  33. public void run() {
  34. String progressMessage = progress + "% izvrseno";
  35. Toast.makeText(getApplicationContext(), progressMessage, Toast.LENGTH_SHORT).show();
  36. }
  37. });
  38. }
  39.  
  40. private void performLongTask() {
  41. try {
  42. Thread.sleep(4000);
  43. } catch (InterruptedException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement