How to implement TvView on Android TV?

purpose

To create a smaller TvView that displays on top of the WebView.
It is specifically for live television.
(I have seen many solutions that relate to streaming content, but this is for video that is transmitted via coaxial cable.)

Layout

Where am I now...

Part of WebView works fine, and I found out that RelativeLayout is required to view layers.
Also, TvView does not seem to be recognized, so I assume that I will probably have to use SurfaceView.

activity_main.xml Code example

MainActivity.java enter image description here

Questions

I have seen examples of TvInput applications, but this is extremely excessive. I am not trying to recreate the TvInput service, I just use the existing infrastructure.

  • , ​​ Android-, ?
  • , ?
  • , ? ( .)

, . .


tung.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_browse_fragment"
    android:name="com.company.app.MainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.company.app.MainActivity"
    tools:deviceIds="tv"
    tools:ignore="MergeRootFrame" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TvView
        android:id="@+id/TvView"
        android:layout_width="816dp"
        android:layout_height="404dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp" />

</RelativeLayout >

MainActivity.java

package com.company.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.media.tv.TvView;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;

public class MainActivity extends Activity {
    private WebView mWebView;
    private TvView mTvView;
    private WebSettings mWebSettings;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        mWebView = new WebView(this);
        mWebSettings = mWebView.getSettings();
        mWebSettings.setJavaScriptEnabled(true);
        mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mWebView.loadUrl("http://10.28.98.150:1000/");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        this.setContentView(mWebView);
        mTvView = new TvView(this );
        mTvView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        this.setContentView(mTvView);
    }

    @Override
    public boolean onKeyDown(final int keyCode, final KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
+4

Source: https://habr.com/ru/post/1695988/


All Articles