title: Stop ringtone of broadcastReceiver from an activity kotlin
body: <p>I've already tried similar question <a href="https://stackoverflow.com/questions/13711856/stop-alarm-from-ringing-in-another-activity-android">stop alarm from ringing in another activity</a>. However, it didn't work in my case. What I want to achieve is:</p>
  <ol>
  <li>When notification fires, ringtone starts in broadcast receiver.</li>
  <li>If device is on, user can stop ringtone pressing button on popup notification.</li>
  <li>If device is off, fullscreen activity turns on and user can stop ringtone pressing stop button in this activity.</li>
  </ol>
  <p>The problem is, when I press stop button in fullscreen activity, the ringtone doesn't stop.</p>
  <p>Here is the code.
  AlarmReceiver:</p>
  <pre><code>class AlarmReceiver: BroadcastReceiver() {
  
          companion object {
              lateinit var ringtoneSound: Ringtone
              val ALARM_CHANNEL_ID = &quot;alarm_channel&quot;
          }
  
          lateinit var notificationManager: NotificationManager
  
          override fun onReceive(context: Context, intent: Intent) {
  
              val alarmId = intent.getIntExtra(&quot;alarmId&quot;, 0)
              val title = intent.getStringExtra(&quot;title&quot;)
              val memo = intent.getStringExtra(&quot;memo&quot;)
              val ringLength = intent.getIntExtra(&quot;ringLength&quot;, 5)
  
  
              notificationManager =
                  context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  
              createAlarmChannel()
  
              val contentIntent = Intent(context, MainActivity::class.java)
              val contentPendingIntent = PendingIntent.getActivity(
                  context, alarmId, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT)
  
              val fullScreenIntent = Intent(context, LockscreenActivity::class.java)
              val fullScreenPendingIntent = PendingIntent.getActivity(context, alarmId, fullScreenIntent, 0)
  
              val deleteIntent = Intent(context, StopAlarmReceiver::class.java).putExtra(&quot;alarmId&quot;, alarmId)
              val deletePendingIntent = PendingIntent.getBroadcast(context,alarmId,deleteIntent,0)
  
              val builder = NotificationCompat.Builder(context, ALARM_CHANNEL_ID)
                  .setSmallIcon(R.drawable.ic_circle_24)
                  .setContentTitle(title)
                  .setContentText(memo)
                  .setColor(ResourcesCompat.getColor(context.resources, R.color.purple_700, null))
                  .setContentIntent(contentPendingIntent)
                  .setFullScreenIntent(fullScreenPendingIntent, true)
                  .setPriority(NotificationCompat.PRIORITY_HIGH)
                  .setCategory(NotificationCompat.CATEGORY_ALARM)
                  .setAutoCancel(true)
                  .setSound(null)
                  .addAction(R.drawable.ic_circle_24, &quot;STOP&quot;, deletePendingIntent)
  
              notificationManager.notify(alarmId, builder.build())
  
              // when notification fired, Ringtone starts.
              ringtoneSound = RingtoneManager.getRingtone(context, getDefaultUri(RingtoneManager.TYPE_ALARM))
              ringtoneSound.play()
  
              // and after certain seconds, Ringtone will stop.
              val countLength = 1000 * ringLength.toLong()
              object : CountDownTimer(countLength, 1000) {
  
                  override fun onTick(millisUntilFinished: Long) {}
  
                  override fun onFinish() {
                      ringtoneSound.stop()
                      notificationManager.cancel(alarmId)
                  }
              }.start()
          }
  
  
      fun createAlarmChannel() {
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
              val alarmChannel = NotificationChannel(ALARM_CHANNEL_ID, &quot;Alarm&quot;, NotificationManager.IMPORTANCE_HIGH)
              alarmChannel.setSound(null, null)
              alarmChannel.enableLights(true)
              alarmChannel.lightColor = Color.RED
              alarmChannel.enableVibration(true)
              alarmChannel.description = &quot;Alarm style&quot;
              alarmChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
              notificationManager.createNotificationChannel(alarmChannel)
          }
      }
  }
  </code></pre>
  <p>StopAlarmReceiver:</p>
  <pre><code>class StopAlarmReceiver: BroadcastReceiver() {
  
          override fun onReceive(context: Context, intent: Intent) {
  
              val alarmId = intent.getIntExtra(&quot;alarmId&quot;, 0)
  
              // This stops alarm when press the button of notification.(It works!)
              AlarmReceiver.ringtoneSound.stop()
  
  
              val notificationManager =
                  context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
              notificationManager.cancel(alarmId)
          }
      }
  </code></pre>
  <p>LockScreenActivity:</p>
  <pre><code>class LockscreenActivity : AppCompatActivity() {
  
      private lateinit var binding: ActivityLockscreenBinding
  
      override fun onCreate(savedInstanceState: Bundle?) {
          showWhenLockedAndTurnScreenOn()
          super.onCreate(savedInstanceState)
          binding = ActivityLockscreenBinding.inflate(layoutInflater)
          setContentView(binding.root)
  
  
          binding.stoppingbutton.setOnClickListener {
  
              // This does not stop Alarm!
              AlarmReceiver.ringtoneSound.stop()
  
              finish()
          }
  
      }
  
  
      private fun showWhenLockedAndTurnScreenOn() {
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O_MR1) {
              setShowWhenLocked(true)
              setTurnScreenOn(true)
          } else {
              window.addFlags(
                  WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                          or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
              )
          }
      }
  }
  </code></pre>
  <p>StopAlarmReceiver is working, however LockScreenActivity doesn't work even though using same code.</p>
  <p>Of course, if I write <code>start()</code> on LockScreenActivity, it works. However when device is on and popup appears, ringtone doesn't start.
  So how can I stop receiver's ringtone from Activity? Any help appreciate.</p>