实现Android TV Input Service时无法创建TvInputInfo的问题咨询
问题解答
是否为已知问题
这是Android框架中明确存在的线程约束问题,属于已知情况。因为HdmiCecController.getPhysicalAddress()被标记为@ServiceThreadOnly,要求必须在HdmiControlService的专属线程调用,而TvInputInfo.Builder.build()在默认线程执行时,会触发这个线程检查断言,抛出IllegalStateException。不少开发者在实现TV Input Service时都碰到过这个线程不匹配的问题。
无需触发断言的创建方法
有两种可行方案:
1. 在HdmiControlService线程内构建TvInputInfo
利用HdmiControlManager.runOnServiceThread()方法,将构建逻辑提交到HdmiControlService的专属线程执行,这样就能绕过线程检查:
Context context = getApplicationContext(); ComponentName componentName = new ComponentName(context, YourTvInputService.class); HdmiDeviceInfo hdmiDeviceInfo = ...; // 你的HDMI设备信息 HdmiControlManager hcm = (HdmiControlManager) context.getSystemService(Context.HDMI_CONTROL_SERVICE); if (hcm != null) { hcm.runOnServiceThread(() -> { TvInputInfo tvInputInfo = new TvInputInfo.Builder(context, componentName) .setHdmiDeviceInfo(hdmiDeviceInfo) .setLabel("你的HDMI输入源") // 添加其他必要配置 .build(); // 这里处理构建好的TvInputInfo,比如注册到系统 }); }
2. 不关联HDMI设备信息(适用于非HDMI输入源)
如果你的TV Input Service不需要关联HDMI设备,可以跳过setHdmiDeviceInfo()方法调用。这样build()流程不会触发getRelativePosition()的逻辑,自然也就不会走到线程断言的代码:
TvInputInfo tvInputInfo = new TvInputInfo.Builder(context, componentName) .setLabel("非HDMI输入源") // 添加其他必要配置 .build();
内容的提问来源于stack exchange,提问作者David Wong
相关产品推荐
相关产品推荐

