6

在我的搜索中,我发现到目前为止,Android SDK 不支持控制 HDMI 端口活动和处理 HDMI 输出。尽管像摩托罗拉这样的某些设备制造商(不知道是否有其他制造商也这样做)提供了 API 以获得更好的控制。以下是其中两个的链接,其中不推荐使用双屏(非常符合我的要求)。

摩托罗拉 HDMI 状态 API

摩托罗拉hdmi双屏api

镜像是连接 HDMI 的默认行为,但我希望我的应用程序在 HDMI 输出上运行绑定服务。这将允许手机同时执行任何其他任务,而不会干扰我在 HDMI 屏幕上运行的服务。

有人可以建议我该怎么做吗?或者是否有任何其他制造商提供与摩托罗拉类似的灵活性?

4

1 回答 1

1

Create a Service class like so.

public class MultiDisplayService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        DisplayManager dm = (DisplayManager)getApplicationContext().getSystemService(DISPLAY_SERVICE);
        if (dm != null){
            Display dispArray[] = dm.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);

        if (dispArray.length>0){
            Display display = dispArray[0];
            Log.e(TAG,"Service using display:"+display.getName());
            Context displayContext = getApplicationContext().createDisplayContext(display);
            WindowManager wm = (WindowManager)displayContext.getSystemService(WINDOW_SERVICE);
            View view = LayoutInflater.from(displayContext).inflate(R.layout.fragment_main,null);
            final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.TYPE_TOAST,
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                    PixelFormat.TRANSLUCENT);
            wm.addView(view, params);
        }
    }
}

Start the service, perhaps in your Application class.

public class MultiDisplayApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        startService(new Intent(this, MultiDisplayService.class));
    }
}

You will probably need more complex display add/remove logic based on DisplayManager.DisplayListener

mDisplayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
mDisplayManager.registerDisplayListener(this, null);

Using WindowManager.LayoutParams.TYPE_TOAST requires no permissions but seems like a hack. WindowManager.LayoutParams.TYPE_SYSTEM_ALERT might be more reasonable, but requieres

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

in your AndroidManifest.

于 2015-08-18T19:56:57.540 回答