Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a handler in Android Studio?

I've been trying to do timed tasks with a handler in Android Studio, but when I try to initialize it, this happens:

private Handler handler = new Handler() {
        @Override
        public void publish(LogRecord record) {

        }

        @Override
        public void flush() {

        }
    @Override
    public void close() throws SecurityException {

    }
};

Whenever I look at online examples where people use Handlers to execute code at intervals, their declarations look as such:

private Handler handler = new Handler();

How do I avoid the big jumble of methods within the Handler?

like image 607
underspring3000 Avatar asked Jan 31 '26 05:01

underspring3000


1 Answers

Looks like you're trying to use java.util.logging.Handler instead of android.os.Handler. Changing which one you import at the top of the file should fix your problem.

The auto-complete should show you which packages you will be importing from, so watch out for that in the future.

like image 120
ctetherton Avatar answered Feb 02 '26 17:02

ctetherton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!