首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Android应用程序设置android电视的输入

使用Android应用程序设置android电视的输入
EN

Stack Overflow用户
提问于 2020-01-27 20:48:40
回答 2查看 1.9K关注 0票数 1

目前,我的HiSense Q8安卓电视有一个启动屏幕,上面有应用程序、Youtube和Netflix图标。为了显示来自HDMI输入1的输入,我需要对遥控器进行一系列的点击。我想要的是一个android应用程序可以在引导时显示HDMI输入1,而不需要我使用遥控器(也就是让它表现得像一个简单的监视器)。但是,我似乎忽略了以编程方式选择HDMI输入的步骤。对相关的源代码示例有什么建议或提示吗?

电视上有android 8,我的目标是应用程序的Android7.1.1。

通过下面的调用,我可以获得要迭代的输入列表:

代码语言:javascript
复制
  TvInputManager mTvInputManager = (TvInputManager) getSystemService(Context.TV_INPUT_SERVICE);
  List<TvInputInfo> inputs = mTvInputManager.getTvInputList();

HDMI TvInputInfo项的id字段如下所示:

代码语言:javascript
复制
"com.mediatex.tvinput/.hdmi.HDMInputService/HW4"
"com.mediatex.tvinput/.hdmi.HDMInputService/HW3"
"com.mediatex.tvinput/.hdmi.HDMInputService/HW2"

然后尝试使用以下方法将显示的输入设置为HW2

代码语言:javascript
复制
TvView view = new TvView(this);
view.tune("com.mediatex.tvinput/.hdmi.HDMInputService/HW2", null);

或者类似于HW4,但是什么都没有发生,我仍然看到了应用程序的显示,而不是HDMI输入。向TvView对象添加回调不会捕获任何错误。在我看来,创建TvView对象的方式有点可疑。

以下是整个代码:

代码语言:javascript
复制
package org.ericdavies.sethdmi1;

import android.app.Activity;
import android.content.Context;
import android.media.tv.TvContentRating;
import android.media.tv.TvTrackInfo;
import android.media.tv.TvView;
import android.os.Bundle;
import android.util.Log;

import android.media.tv.TvContract;
import android.net.Uri;

import android.media.tv.TvInputManager;
import android.media.tv.TvInputInfo;

import android.media.tv.TvInputService;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

/*
 * Main Activity class that loads {@link MainFragment}.
 */
public class MainActivity extends Activity {

    TvView view;
    TextView tv;
    StringBuilder sb;

    private void setUpButton(final String inputId, int buttonTag) {
        Button bt = findViewById(R.id.buttonhw4);
        bt.setEnabled(true);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                view.setCallback(new TvView.TvInputCallback() {

                });
                view.tune(inputId, null);
            }
        });
    }

    public void reportState(final String state) {
        this.runOnUiThread(new Runnable() {
            public void run() {
                sb.append(state);
                tv.setText(sb.toString());
            }
        });
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TvInputManager mTvInputManager = (TvInputManager) getSystemService(Context.TV_INPUT_SERVICE);
        sb = new StringBuilder();
        List<TvInputInfo> inputs = mTvInputManager.getTvInputList();
        view = new TvView(this);
        tv = findViewById(R.id.mytextfield);

        view.setCallback(new TvView.TvInputCallback() {
                             @Override
                             public void onConnectionFailed(String inputId) {
                                 super.onConnectionFailed(inputId);
                                 reportState("tvview.onconnectionFailed\n");
                             }

                             @Override
                             public void onDisconnected(String inputId) {
                                 super.onDisconnected(inputId);
                                 reportState("tvview.onDisconnected\n");
                             }

                             @Override
                             public void onChannelRetuned(String inputId, Uri channelUri) {
                                 super.onChannelRetuned(inputId, channelUri);
                                 reportState("tvview.onChannelRetuned\n");
                             }

                             @Override
                             public void onTracksChanged(String inputId, List<TvTrackInfo> tracks) {
                                 super.onTracksChanged(inputId, tracks);
                                 reportState("tvview.onTracksChanged\n");
                             }

                             @Override
                             public void onTrackSelected(String inputId, int type, String trackId) {
                                 super.onTrackSelected(inputId, type, trackId);
                                 reportState("tvview.onTrackSelected\n");
                             }

                             @Override
                             public void onVideoUnavailable(String inputId, int reason) {
                                 super.onVideoUnavailable(inputId, reason);
                                 reportState("tvview.onVideoUnavailable\n");
                             }

                             @Override
                             public void onContentBlocked(String inputId, TvContentRating rating) {
                                 super.onContentBlocked(inputId, rating);
                                 reportState("tvview.onContentBlocked\n");
                             }
                         }
                );

        for (TvInputInfo input : inputs) {
            String id = input.getId();
            if( input.isPassthroughInput() && id.contains("HDMIInputService")) {
                sb.append("inputid = " + input.getId() + "\n");
                if( id.contains("HW4")) {
                    setUpButton(id, R.id.buttonhw4);
                }
                if( id.contains("HW2")) {
                    setUpButton(id, R.id.buttonHw2);
                }
                if( id.contains("HW3")) {
                    setUpButton(id, R.id.buttonhw3);
                }
            }
        }

        if( tv != null) {
            tv.setText(sb.toString());
        }

    }
}

布局是

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:visibility="visible">

    <EditText
        android:id="@+id/mytextfield"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoText="false"
        android:clickable="false"
        android:editable="false"
        android:ems="10"
        android:enabled="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="start|top"
        android:inputType="textMultiLine"
        android:selectAllOnFocus="false"
        android:text="-----" />

    <Button
        android:id="@+id/buttonhw4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hw4" />

    <Button
        android:id="@+id/buttonHw2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hw2" />

    <Button
        android:id="@+id/buttonhw3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="hw3" />
</LinearLayout>

清单是

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="org.ericdavies.sethdmi1">

    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />
    <uses-feature
        android:name="android.software.leanback"
        android:required="true" />
    <uses-feature
        android:name="android.software.LIVE_TV"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:banner="@drawable/app_icon_your_company"
            android:icon="@drawable/app_icon_your_company"
            android:label="@string/app_name"
            android:logo="@drawable/app_icon_your_company"
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DetailsActivity" />
        <activity android:name=".PlaybackActivity" />
        <activity android:name=".BrowseErrorActivity" />
    </application>
</manifest>

应用程序的build.gradle是

代码语言:javascript
复制
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "org.ericdavies.sethdmi1"
        minSdkVersion 25
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.leanback:leanback:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.github.bumptech.glide:glide:3.8.0'
}
EN

回答 2

Stack Overflow用户

发布于 2022-06-30 16:59:08

而不是对于第二个参数(URI)为null:

代码语言:javascript
复制
TvView view = new TvView(this);
view.tune("com.mediatex.tvinput/.hdmi.HDMInputService/HW2", null);

您需要创建和发送一个有效的Uri:

代码语言:javascript
复制
TvView view = new TvView(this)
mInitChannelUri = TvContract.buildChannelUriForPassthroughInput("com.mediatex.tvinput/.hdmi.HDMInputService/HW2")
view.tune("com.mediatex.tvinput/.hdmi.HDMInputService/HW2", mInitChannelUri)

这有点傻,因为您基本上发送了相同的输入名称字符串两次。但这对我有用。

最后,要想独立于TV品牌,您应该使用输入id参数而不是静态字符串常量(留给读者的练习是哈哈)。

票数 1
EN

Stack Overflow用户

发布于 2022-08-05 18:44:26

您不需要使用TvView。只需对ACTION_VIEW使用隐式意图即可。

我在我的索尼电视上测试了这个代码,它运行得很好。(我在用Kotlin)

代码语言:javascript
复制
// Passthrough inputs are "hardware" inputs like HDMI / Components. Non-passthrough input are
// usually internal tv tuners. You can also filter out non-passthrough inputs before this step.
val uri =
    if (inputInfo.isPassthroughInput) TvContract.buildChannelUriForPassthroughInput(inputInfo.id)
    else TvContract.buildChannelsUriForInput(inputInfo.id)

val intent = Intent(Intent.ACTION_VIEW, uri)

if (intent.resolveActivity(packageManager) != null) {
  context.startActivity(intent)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59938698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档