title: Pass data to Service in Kotlin
body: <p>I have an app that uses Service class to perform task in foreground.</p>
  
  <p>This service also contains a Handler object to run same function multiple times. I want to change attributes in my activity_main.xml while functions are running in Service. For example when function calculates something in Service the result prints in TextView.</p>
  
  <p>How it would be correct access activity_main's objects to retrieve and change their values and attributes?</p>
  
  <p>Here is what I have:</p>
  
  <p><strong>MainActivity.kt</strong>:</p>
  
  <pre><code>class MainActivity : AppCompatActivity() {
      private var notificationManager: NotificationManager? = null
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          buttonStart.setOnClickListener{
              buttonStart.isEnabled = false
              buttonStop.isEnabled = true
              IdListener.startService(this, "Foreground Service is running...")
          }
      }
  }
  </code></pre>
  
  <p><strong>IdListener.kt</strong>:</p>
  
  <pre><code>class IdListener : Service() {
      private val CHANNEL_ID = "ForegroundService Kotlin"
      private lateinit var mainHandler: Handler
      private lateinit var mRunnable: Runnable
  
      companion object {
          fun startService(context: Context, message: String) {
              val startIntent = Intent(context, IdListener::class.java)
              startIntent.putExtra("inputExtra", message)
              ContextCompat.startForegroundService(context, startIntent)
          }
          fun stopService(context: Context) {
              val stopIntent = Intent(context, IdListener::class.java)
              context.stopService(stopIntent)
          }
      }
  
      override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  
          mainHandler = Handler()
          mRunnable = Runnable { showRandomNumber(tm) }
          mainHandler.postDelayed(mRunnable, 1000)
  
          val input = intent?.getStringExtra("inputExtra")
          createNotificationChannel()
          val notificationIntent = Intent(this, MainActivity::class.java)
          val pendingIntent = PendingIntent.getActivity(
              this,
              0, notificationIntent, 0
          )
          val notification = NotificationCompat.Builder(this, CHANNEL_ID)
              .setContentTitle("Foreground Service Kotlin Example")
              .setContentText(input)
              .setSmallIcon(R.drawable.ic_notofication)
              .setContentIntent(pendingIntent)
              .build()
          startForeground(1, notification)
          return START_NOT_STICKY
      }
      override fun onBind(intent: Intent): IBinder? {
          return null
      }
  
      override fun onDestroy() {
          super.onDestroy()
          mainHandler.removeCallbacks(mRunnable)
      }
  
      private fun createNotificationChannel() {
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
              val serviceChannel = NotificationChannel(CHANNEL_ID, "Foreground Service Channel",
                  NotificationManager.IMPORTANCE_DEFAULT)
              val manager = getSystemService(NotificationManager::class.java)
              manager!!.createNotificationChannel(serviceChannel)
          }
      }
  
      /// function in which I want elements from activity_main.xml to be changed
      fun showRandomNumber(manager: TelephonyManager){
          myTextView.text = "Working..." 
          mainHandler.postDelayed(mRunnable, 1000)
      }
  
  }
  </code></pre>