【问题标题】:MediaRouter - Display content to second screenMediaRouter - 将内容显示到第二个屏幕
【发布时间】:2017-12-13 11:45:35
【问题描述】:

我是 Android 新手,正在努力学习 - 请不要乱扔石头。

感谢@commonsguy 的精彩示例: https://github.com/commonsguy/cw-omnibus/blob/master/Presentation/Simple/app/src/main/java/com/commonsware/android/preso/simple/MainActivity.java

我可以将网站显示到辅助显示器(我的情况是 Miracast 外接显示器)。

  1. 当前网站已显示,但我无法与之交互(向上、向下滚动、单击链接、输入用户名、密码等)。有没有办法让这个区域作为输入准备好? (附有硬件键盘和鼠标,希望能够使用它们来导航页面)

已从 Why is Android WebView refusing user input? 添加了逻辑,但也没有帮助。

/**
 Copyright (c) 2013 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 Covered in detail in the book _The Busy Coder's Guide to Android Development_
 https://commonsware.com/Android
 */

package com.example.danielsz.simpledisplay;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Presentation;
import android.content.Context;
import android.media.MediaRouter;
import android.media.MediaRouter.RouteInfo;
import android.media.MediaRouter.SimpleCallback;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;

public class MainActivity extends Activity {
    MediaRouter router=null;
    Presentation preso=null;
    SimpleCallback cb=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onStart() {
        super.onStart();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (cb==null) {
                cb=new RouteCallback();
                router=(MediaRouter)getSystemService(MEDIA_ROUTER_SERVICE);
            }

            handleRoute(router.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO));
            router.addCallback(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, cb);
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onStop() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            clearPreso();

            if (router != null) {
                router.removeCallback(cb);
            }
        }

        super.onStop();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void handleRoute(RouteInfo route) {
        if (route == null) {
            clearPreso();
        }
        else {
            Display display=route.getPresentationDisplay();

            if (route.isEnabled() && display != null) {
                if (preso == null) {
                    showPreso(route);
                    Log.d(getClass().getSimpleName(), "enabled route");
                }
                else if (preso.getDisplay().getDisplayId() != display.getDisplayId()) {
                    clearPreso();
                    showPreso(route);
                    Log.d(getClass().getSimpleName(), "switched route");
                }
                else {
                    // no-op: should already be set
                }
            }
            else {
                clearPreso();
                Log.d(getClass().getSimpleName(), "disabled route");
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void clearPreso() {
        if (preso != null) {
            preso.dismiss();
            preso=null;
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void showPreso(RouteInfo route) {
        preso=new SimplePresentation(this, route.getPresentationDisplay());
        preso.show();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private class RouteCallback extends SimpleCallback {
        @Override
        public void onRoutePresentationDisplayChanged(MediaRouter router,
                                                      RouteInfo route) {
            handleRoute(route);
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private class SimplePresentation extends Presentation {
        SimplePresentation(Context ctxt, Display display) {
            super(ctxt, display);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            WebView wv=new WebView(getContext());


            wv.getSettings().setJavaScriptEnabled(true);
            wv.loadUrl("https://ctxtsprodeme.xendesktop.net/");

            setContentView(wv);
        }
    }
}

非常感谢。 丹尼尔

【问题讨论】:

  • 我得到 Display display=route.getPresentationDisplay(); null 怎么解决?

标签: android mediarouter


【解决方案1】:

有没有办法让这个区域准备好输入?

不,对不起。外部显示器在标准 Android 中仅显示。您可以在主显示器(例如,您的手机或平板电脑)上收集输入并使用它来影响外部显示器上的内容(例如,在WebView 上致电pageDown()pageUp()),仅此而已。

【讨论】:

  • 我得到 Display display=route.getPresentationDisplay(); null 怎么解决?
  • @RavindraPawar:我建议您提出一个单独的 Stack Overflow 问题,并提供 minimal reproducible example
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 1970-01-01
  • 2020-11-08
  • 1970-01-01
  • 2019-09-11
  • 2014-04-30
  • 1970-01-01
相关资源
最近更新 更多