android11监测两路HDMI输出信号代码实现
时间: 2023-12-16 19:02:28 浏览: 215
实现 Android 11 监测两路 HDMI 输出信号的代码如下:
```
private final BroadcastReceiver mHdmi1Receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HDMI_PLUGGED)) {
int state = intent.getIntExtra(HdmiManager.EXTRA_HDMI_PLUG_STATE, -1);
if (state == HdmiManager.HDMI_PLUGGED) {
// HDMI 1 is plugged in
} else if (state == HdmiManager.HDMI_UNPLUGGED) {
// HDMI 1 is unplugged
}
}
}
};
private final BroadcastReceiver mHdmi2Receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HDMI_PLUGGED)) {
int state = intent.getIntExtra(HdmiManager.EXTRA_HDMI_PLUG_STATE, -1);
if (state == HdmiManager.HDMI_PLUGGED) {
// HDMI 2 is plugged in
} else if (state == HdmiManager.HDMI_UNPLUGGED) {
// HDMI 2 is unplugged
}
}
}
};
private void registerHdmiReceivers() {
IntentFilter hdmi1Filter = new IntentFilter(Intent.ACTION_HDMI_PLUGGED);
IntentFilter hdmi2Filter = new IntentFilter(Intent.ACTION_HDMI_PLUGGED);
hdmi1Filter.addAction(Intent.ACTION_HDMI_PLUGGED);
hdmi2Filter.addAction(Intent.ACTION_HDMI_PLUGGED);
hdmi1Filter.addCategory(Intent.CATEGORY_DEFAULT);
hdmi2Filter.addCategory(Intent.CATEGORY_DEFAULT);
hdmi1Filter.addDataScheme("hdmi");
hdmi2Filter.addDataScheme("hdmi");
registerReceiver(mHdmi1Receiver, hdmi1Filter);
registerReceiver(mHdmi2Receiver, hdmi2Filter);
}
```
以上代码实现了注册两个 BroadcastReceiver 监听 HDMI 1 和 HDMI 2 的插拔事件,当 HDMI 1 或 HDMI 2 被插入或拔出时,会触发相应的回调函数,您可以在回调函数中执行相应的操作。请注意,此代码适用于 Android 11 及以上版本。
阅读全文