This paper considers a scenario in which a secondary user makes opportunistic use of a channel allocated to some primary network. The primary network operates in a time-slotted manner and switches between idle and active states according to a stationary Markovian process. At the beginning of each time slot, the secondary user can choose to stay idle or to carry out spectrum sensing to detect if the primary network is idle or active. If the primary network is detected as idle, the secondary user can carry out data transmission. Spectrum sensing consumes time and energy and introduces false alarms and mis-detections. Given the delay cost associated with staying idle, the energy costs associated with spectrum sensing and data transmission, and the throughput gain associated with successful transmissions, the objective is to decide, for each time slot, whether the secondary user should stay idle or carry out sensing, and if so, for how long, to maximize the expected net reward. We formula...