Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gorsek.filip.services;
- import android.app.IntentService;
- import android.content.Intent;
- import android.os.Handler;
- import android.widget.Toast;
- public class MyIntentService extends IntentService {
- private Handler handler = new Handler();
- public MyIntentService() {
- super("MyIntentService");
- }
- @Override
- protected void onHandleIntent(Intent intent) {
- if (intent != null) {
- int taskCount = intent.getIntExtra(MainActivity.TASK_COUNT, 0);
- for (int i = 0; i < taskCount; i++) {
- performLongTask();
- int progress = ((int) ((i + 1) / (double) taskCount * 100));
- showProgress(progress);
- }
- }
- }
- private void showProgress(final int progress) {
- handler.post(new Runnable() {
- @Override
- public void run() {
- String progressMessage = progress + "% izvrseno";
- Toast.makeText(getApplicationContext(), progressMessage, Toast.LENGTH_SHORT).show();
- }
- });
- }
- private void performLongTask() {
- try {
- Thread.sleep(4000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement