You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oreo悬浮来电通知:检测滑入状态栏操作以停止铃声

Fix: Stop Ringtone When Incoming Call Notification is Swiped to Status Bar

Hey fellow dev, let me walk you through how I solved this exact issue with incoming call notifications!

The Problem I Faced

I was creating an incoming call notification using NotificationCompat.Builder with these configurations:

  • Set priority to PRIORITY_HIGH
  • Enabled vibration
  • Set ongoing to true

When I called notify(), the ringtone started playing as expected, and the answer/decline functions worked flawlessly. But there was one frustrating edge case: when the user swiped the floating heads-up notification into the status bar, the ringtone continued playing—I had no way to detect that action to stop it manually.

The Simple Solution

Turns out the fix is way easier than trying to listen for notification state changes: instead of manually launching a ringtone player, use the built-in .setSound() method on the NotificationCompat.Builder to assign your ringtone.

Example Code

// Make sure you've already set up your NotificationManager and NotificationChannel first
val callNotificationBuilder = NotificationCompat.Builder(context, CALL_CHANNEL_ID)
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setVibrate(longArrayOf(500, 500, 500)) // Customize your vibration pattern here
    .setOngoing(true)
    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)) // This line fixes the issue
    // Don't forget required settings like small icon, content title, and content text
    .setSmallIcon(R.drawable.ic_call_notification)
    .setContentTitle("Incoming Call")
    .setContentText("John Doe is calling")

// Trigger the notification
notificationManager.notify(CALL_NOTIFICATION_ID, callNotificationBuilder.build())

Why This Works

By letting Android's system handle the ringtone via .setSound(), you get automatic lifecycle management out of the box:

  • The ringtone plays while the notification is in heads-up (floating) mode
  • As soon as the user swipes the notification into the status bar, the system automatically stops the ringtone
  • When you cancel the notification (e.g., when the call is answered or declined), the ringtone stops immediately too

This approach eliminates the need to write custom listeners for notification state changes, and ensures your app behaves consistently with the system's native call notifications—something users will appreciate.

内容的提问来源于stack exchange,提问作者devchimp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:39:23