一款应用中的AndroidTv和智能手机/不同的启动器 [英] AndroidTv and Smartphone in one app / Different launcher

查看:124
本文介绍了一款应用中的AndroidTv和智能手机/不同的启动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个可用于电视和智能手机的应用程序.现在,我只想根据设备来区分主屏幕. 我放置了片段,因此根据设备的不同安排了它们. 但是当我在电视上启动该应用程序时,不是启动tvActivity而是mainActivity

I am writing an application that will serve for tv and smartphone. For now I just want to differentiate the main screen depending on the device. I have placed fragments, and that are therefore arranged differently according to the device. But when I launch the application on TV, it is not tvActivity that is launched but the mainActivity

此外,清单中有2个发射器,一个

Also, i have 2 launcher in the manifest, one

<action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

和另一个

<action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

这是我的代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jamesp.fragmenttest">

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


    <!--Unsupported TV hardware features-->
    <uses-feature android:name="android.hardware.touchscreen"
        android:required="false"/>
    <uses-feature android:name="android.hardware.faketouch"
        android:required="false"/>
    <uses-feature android:name="android.hardware.telephony"
        android:required="false"/>
    <uses-feature android:name="android.hardware.camera"
        android:required="false"/>
    <uses-feature android:name="android.hardware.nfc"
        android:required="false"/>
    <uses-feature android:name="android.hardware.location.gps"
        android:required="false"/>
    <uses-feature android:name="android.hardware.microphone"
        android:required="false"/>
    <uses-feature android:name="android.hardware.sensor"
        android:required="false"/>



    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:banner="@mipmap/ic_launcher">



        <activity android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity android:name=".TvActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.Leanback">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

</manifest>

TvActivity:

TvActivity :

public class TvActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tv);

    }
}

MainActivity:

MainActivity :

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "TAG";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
         if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
            Log.d(TAG, "Running on a TV Device");
            setContentView(R.layout.activity_tv);
        } else {
            setContentView(R.layout.activity_main);
            Log.d(TAG, "Running on a non-TV Device");
        }
    }
}

通过此测试,我可以区分并因此选择正确的布局,但是我不理解电视为什么启动MainActivity.

With this test I can differentiate and therefore choose the right layout, but I don't understand why Tv is launching MainActivity.

谢谢

推荐答案

将此用作清单.会起作用的.

Use this as your manifest. It will work.

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

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

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


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.Leanback">
    <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.LAUNCHER" />
            <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DetailsActivity" />
    <activity android:name=".PlaybackOverlayActivity" />
    <activity android:name=".BrowseErrorActivity" />
</application>

这篇关于一款应用中的AndroidTv和智能手机/不同的启动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆