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

使用投屏功能时因Support Library版本冲突引发崩溃求助

解决Cast Companion Library与高版本Support Library的通知构造方法冲突崩溃问题

问题根源

你遇到的崩溃是因为**Google Cast Companion Library(cast-service-framework 15.0.1)**里的VideoCastNotificationService还在调用已经被移除的NotificationCompat.Builder(Context)构造方法——这个方法在Support Library 26.0.0被标记为废弃,27.0.0版本直接被删除了。而你又必须使用高版本的Support Library(27.1.1)来适配最新Android系统,这就导致了类找不到的崩溃。

可行解决方案:自定义通知服务替代原类

由于官方库暂时没修复这个问题,我们可以通过继承并重写关键方法来绕过这个坑:

  1. 创建自定义的Cast通知服务类
    继承原VideoCastNotificationService,重写build()方法,改用带Channel ID的合法构造方法:

    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.content.Context;
    import android.os.Build;
    import com.google.android.libraries.cast.companionlibrary.notification.VideoCastNotificationService;
    import android.support.v7.app.NotificationCompat;
    
    public class CustomCastNotificationService extends VideoCastNotificationService {
    
        // 自定义通知渠道ID,可根据需求修改
        private static final String CAST_NOTIFICATION_CHANNEL_ID = "cast_notification_channel";
    
        @Override
        protected void build() {
            // 先为Android O及以上版本创建通知渠道(必填)
            createNotificationChannelIfNeeded();
    
            // 使用带Channel ID的构造方法创建Builder,替代原废弃方法
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CAST_NOTIFICATION_CHANNEL_ID);
            // 复用父类的通知配置逻辑
            setupNotificationBuilder(builder);
            // 调用父类方法更新通知
            super.updateNotification(builder.build());
        }
    
        private void createNotificationChannelIfNeeded() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                if (notificationManager != null && notificationManager.getNotificationChannel(CAST_NOTIFICATION_CHANNEL_ID) == null) {
                    NotificationChannel channel = new NotificationChannel(
                            CAST_NOTIFICATION_CHANNEL_ID,
                            "投屏通知",
                            NotificationManager.IMPORTANCE_LOW
                    );
                    notificationManager.createNotificationChannel(channel);
                }
            }
        }
    }
    
  2. 在Manifest中替换原服务
    把原来注册的VideoCastNotificationService换成你自定义的类:

    <service
        android:name=".CustomCastNotificationService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.cast.framework.action.RECEIVE"/>
        </intent-filter>
    </service>
    
  3. 配置CastOptions使用自定义服务
    初始化Cast框架时,指定使用我们的自定义通知服务:

    CastOptions castOptions = new CastOptions.Builder()
            .setReceiverApplicationId(getString(R.string.your_cast_app_id)) // 替换成你的Cast应用ID
            .setNotificationOptions(new NotificationOptions.Builder()
                    .setNotificationServiceClass(CustomCastNotificationService.class)
                    .build())
            .build();
    CastContext.getSharedInstance(this, castOptions);
    

补充说明

  • 后续如果Google更新了Cast Companion Library修复了这个问题,你可以直接切回官方的服务类。
  • 要确保自定义类的包路径、Manifest和CastOptions中的引用完全一致,避免出现找不到类的情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:34:10