import android.content.pm.PackageManager
import android.os.Binder
import android.os.IBinder
-import android.telephony.PhoneStateListener
+import android.telephony.PhoneStateListener // This can be replaced by `TelephonyCallback` once the minimum API >= 31.
import android.telephony.TelephonyDisplayInfo
import android.telephony.TelephonyManager
// Define the class variables.
private var currentStatus = ""
- private lateinit var phoneStateListener: PhoneStateListener
+ private lateinit var phoneStateListener: PhoneStateListener // The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
inner class ServiceBinder : Binder() {
// Get a copy of this service as a binder.
// Start the foreground notification.
startForeground(NOTIFICATION_ID, notificationBuilder.build())
- // Define the phone state listener.
+ // Define the phone state listener. The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
phoneStateListener = object : PhoneStateListener() {
override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
// Populate the notification according to the network type.
// Get a handle for the telephony manager.
val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
- // Cancel the current listener if it exists.
+ // Cancel the current listener if it exists. The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
- // Listen for changes to the phone state.
+ // Listen for changes to the phone state. The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)
}
}