- // Get the network registration info for the voice network, which is the second of the three entries (the first appears to be Wi-Fi and the third appears to be the cell data network).
- val networkRegistrationInfo = serviceState.networkRegistrationInfoList[1]
-
- // Get the consider 3G antiquated preference.
- val consider3gAntiquated = sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false)
-
- // Update the voice network security status.
- voiceNetworkSecurityStatus = protocolHelper.checkNetwork(networkRegistrationInfo.accessNetworkTechnology, consider3gAntiquated)
-
- // Populate the notification.
- populateNotification()
+ // Check to see if realtime monitoring is enabled. Sometimes the system keeps running the service even when it is supposed to shut down.
+ if (sharedPreferences.getBoolean(applicationContext.getString(R.string.realtime_monitoring_key), true)) { // Realtime monitoring is enabled.
+ // Get the network registration info for the voice network, which is the second of the three entries (the first appears to be Wi-Fi and the third appears to be the cell data network).
+ val networkRegistrationInfo = serviceState.networkRegistrationInfoList[1]
+
+ // Get the consider 3G antiquated preference.
+ val consider3gAntiquated = sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false)
+
+ // Update the voice network security status.
+ voiceNetworkSecurityStatus = protocolHelper.checkNetwork(networkRegistrationInfo.accessNetworkTechnology, consider3gAntiquated)
+
+ // Populate the notification.
+ populateNotification()
+ } else { // Realtime monitoring is disabled.
+ // Cancel the current listener if it exists. The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
+ telephonyManager.listen(phoneStateListener, LISTEN_NONE)
+ }