- // Register the telephony manager listener.
- registerTelephonyManagerListener()
-
- // Create a register realtime listener work request that fires every 15 minutes with a 1 minute initial delay.
- val registerRealtimeListenerWorkRequest = PeriodicWorkRequestBuilder<RegisterRealtimeListener>(15, TimeUnit.MINUTES).setInitialDelay(1, TimeUnit.MINUTES).build()
-
- // Register the realtime listener work request.
- WorkManager.getInstance(this).enqueueUniquePeriodicWork(getString(R.string.register_listener_work_request), ExistingPeriodicWorkPolicy.REPLACE, registerRealtimeListenerWorkRequest)
-
- // Return a sticky service.
- return START_STICKY
- }
-
- fun registerTelephonyManagerListener() {
- // Get a handle for the telephony manager.
- val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
-
- // Get a handle for the notification manager.
- val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
-
- // Create an intent to open Privacy Cell.
- val privacyCellIntent = Intent(this, PrivacyCellActivity::class.java)
-
- // Create a pending intent from the Privacy Cell intent.
- val privacyCellPendingIntent = PendingIntent.getActivity(this, 0, privacyCellIntent, PendingIntent.FLAG_IMMUTABLE)
-
- // Listen for changes to the phone state.
- telephonyManager.listen(object : PhoneStateListener() {