How to pause a progress of a Notification?

0

There is a ProgressBar in my notification that works like this:

new Thread(
            new Runnable() {
                @Override
                public void run() {
                    int incr;

                        // Do the "lengthy" operation 20 times
                        for (incr = 0; incr <= 100; incr += 5) {
                            // Sets the progress indicator to a max value, the
                            // current completion percentage, and "determinate"
                            // state
                            builder.setProgress(100, incr, false);
                            // Displays the progress bar for the first time.
                            m.notify(id, builder.build());
                            // Sleeps the thread, simulating an operation
                            // that takes time

                            try {
                                // Sleep for 5 seconds
                                Thread.sleep(5 * 1000);
                            } catch (InterruptedException e) {


                        }
                    }

And the two buttons:

builder.addAction(R.drawable.Continuar, "Continuar", ??);
    builder.addAction(R.drawable.Pausar,"Pausar",??);

Would anyone know how to make the buttons pause and summarize Thread?

    
asked by anonymous 31.12.2015 / 05:10

1 answer

0

I'll give you an idea on the object that creates thread, declare a Boolean instance variable 'pause', and on Thread:

new Thread(
        new Runnable() {
            @Override
            public void run() {
                int incr;

                    // Do the "lengthy" operation 20 times
                    for (incr = 0; incr <= 100; incr += 5) {
                        // Sets the progress indicator to a max value, the
                        // current completion percentage, and "determinate"
                        // state
                        builder.setProgress(100, incr, false);
                        // Displays the progress bar for the first time.
                        m.notify(id, builder.build());
                        // Sleeps the thread, simulating an operation
                        // that takes time
                        do{
                            try {
                              // Sleep for 5 seconds
                              Thread.sleep(5 * 1000);

                            } catch (InterruptedException e) {
                                //Quase improvavel de ocorrer
                            }
                        }while(pausar);
                    }
                }
});

And then just change 'pause' to true or false, by the buttons, that it will stay in the loop until you unpair. I used DO-WHILE to run sleep at least once.

    
31.12.2015 / 14:51