How to pause a Notification progress?

Asked

Viewed 41 times

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",??);

Someone would know how to make the buttons pause and summarize the Thread?

1 answer

0

I’ll give you an idea, in the thread-creating object ,declare a Boolean instance variable 'pause', and in 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, it will stay in the loop until you pause. I used DO-WHILE to run Sleep at least once.

  • Ah, thank you very much, but one thing, I don’t quite understand how to change this by the Notification buttons, because I can only get them to open new Classes...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.