Do you want to measure the 24 V or simply detect the presence of the 24 V?
To measure your approach using the divider is o.k.
To detect you can use an optocoupler. Drive the LED by a series resistor, connect the output (transistor) to an input pin of the mcu. But why would you use an optocoupler? Battery and mcu share a common ground, so there is no need for potential isolation. You could as well use a voltage divider to detect the presence of the 24 V. Additional protection for the mcu can be achieved by a zener diode in parallel to the mcu input or a pair of diodes as
shown e.g.here (you will not need the series resistor shown as your voltage divider fulfills the same purpose).