AndroidTv and Smartphone in one app / Different la

2019-08-20 10:10发布

问题:

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

Also, i have 2 launcher in the manifest, one

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

and the other

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

Here is my code :

<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 :

public class TvActivity extends Activity {

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

    }
}

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");
        }
    }
}

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

Thanks

回答1:

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>