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

Android集成Google Cast后,如何禁用默认通知仅用自定义通知?

如何禁用Google Cast的默认通知并使用自定义通知?

当然可以做到!我之前帮不少开发者解决过这个问题,核心是通过Cast SDK的配置关闭默认通知,再自己监听会话状态来维护自定义通知。具体步骤如下:

1. 初始化CastContext时禁用默认媒体通知

这是最关键的一步,直接告诉Cast SDK不要生成自带的通知。在你的Application类或者初始化Cast的地方,配置CastOptions时加上禁用默认通知的参数:

val castOptions = CastOptions.Builder()
    .setReceiverApplicationId(YOUR_RECEIVER_APP_ID) // 替换成你的接收器App ID
    .setStopReceiverApplicationWhenEndingSession(true)
    // 核心配置:关闭Cast自带的媒体通知
    .setEnableMediaNotifications(false)
    .build()
CastContext.setSharedInstance(applicationContext, castOptions)

2. 自定义SessionManagerListener监听Cast会话状态

关掉默认通知后,你需要自己监听Cast会话的生命周期和媒体状态变化,来同步更新你的自定义通知。比如创建一个监听类:

class CustomCastSessionListener(private val service: YourMediaPlaybackService) : SessionManagerListener<CastSession> {

    override fun onSessionStarted(session: CastSession, sessionId: String) {
        // 投屏会话开始,更新自定义通知为投屏状态
        service.updateCustomNotification(isCasting = true)
    }

    override fun onSessionEnded(session: CastSession, error: Int) {
        // 投屏会话结束,切回本地播放的通知状态
        service.updateCustomNotification(isCasting = false)
    }

    override fun onMediaStatusUpdated(session: CastSession, mediaStatus: MediaStatus) {
        // 媒体状态变化(播放/暂停/进度更新),同步到自定义通知
        val isPlaying = mediaStatus.isPlaying
        val currentProgress = mediaStatus.currentPosition
        val totalDuration = mediaStatus.mediaInfo?.contentDuration ?: 0
        service.updateCustomNotification(
            isCasting = true,
            isPlaying = isPlaying,
            progress = currentProgress,
            duration = totalDuration
        )
    }

    // 其他生命周期方法(比如会话暂停/恢复)也可以根据需求监听
    override fun onSessionResumed(session: CastSession, wasSuspended: Boolean) {
        service.updateCustomNotification(isCasting = true)
    }
}

然后记得在你的媒体Service或者初始化代码里注册这个监听器:

val castContext = CastContext.getSharedInstance(applicationContext)
val sessionManager = castContext.sessionManager
val customListener = CustomCastSessionListener(this)
sessionManager.addSessionManagerListener(customListener, CastSession::class.java)

3. 完善自定义通知的投屏交互逻辑

在你的自定义通知里,你可以添加投屏相关的操作按钮(比如暂停投屏、断开连接),通过PendingIntent调用Cast会话的对应方法。比如:

// 断开投屏的PendingIntent
val disconnectIntent = Intent(this, YourMediaPlaybackService::class.java).apply {
    action = ACTION_DISCONNECT_CAST
}
val disconnectPendingIntent = PendingIntent.getService(
    this,
    0,
    disconnectIntent,
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

// 在通知构建器里添加按钮
val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
    .setContentTitle("正在投屏播放")
    .setSmallIcon(R.drawable.ic_cast)
    .addAction(R.drawable.ic_disconnect, "断开投屏", disconnectPendingIntent)
    // 其他通知配置...

然后在你的Service里处理这个动作:

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    when (intent?.action) {
        ACTION_DISCONNECT_CAST -> {
            val castSession = CastContext.getSharedInstance(this).sessionManager.currentCastSession
            castSession?.endSession(true)
        }
        // 其他动作处理...
    }
    return super.onStartCommand(intent, flags, startId)
}

注意事项

  • 确保你的媒体Service是前台Service,并且持有POST_NOTIFICATIONS权限(Android 13及以上需要动态申请),避免被系统回收。
  • 如果需要同步投屏的播放进度,记得在onMediaStatusUpdated里实时更新通知的进度条。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 00:28:11