📜 ⬆️ ⬇️

We launch our game on Android TV

Device Lab from Google distributes all sorts of interesting devices for testing, which was a sin not to use. I wanted to study the work with Android TV. I have applied for testing Android TV. As an experimental application - a puzzle, a mathematical game Mathable for Android. Description of the conditions for obtaining devices here . The application was approved, went to the office of Habr took the test device - Forge TV from Razer .


Article author Barkalov Dmitry, in the framework of the competition "Device Lab from Google . "

Completion of the application to support Android TV


It is necessary to announce the activity that will be launched first on TV. It should be in portrait orientation without bar status and toolbar. I just have this, so we add an intent filter to the existing one.

<activity android:name=".ui.LauncherActivity" android:label="@string/app_name" android:banner="@drawable/ic_banner"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LEANBACK_LAUNCHER" /> </intent-filter> </activity> 

For the application to appear in the corresponding section, add android: isGame = "true"
')
 <application android:name=".MathableApplication" android:label="@string/app_name" android:theme="@style/AppTheme" android:isGame="true"> 

After that, you can run the application for the test. But it is necessary to take into account that there is no touch screen on TV and it is necessary to manage with one remote control.

The selection of control is done by moving the focus. Therefore, for example, for buttons, the state of focus is added to the background selector.

 <?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_disabled" android:state_enabled="false"/> <item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/btn_focused" android:state_focused="true"/> <item android:drawable="@drawable/btn_normal"/> </selector> 

To control the navigation, you need to specify which elements can have focus and the order of moving the focus. How to do it is written here .

Connect Android-TV to a laptop to install and run the application


The application runs on an emulator, I want to run the application on a real device. Of course, you can transfer apk over the network or publish a beta in the play store. But this is a long time, and suddenly debugging will be needed, so only the postings, only hardcore!

On TV there is a USB port that can work as a host and as a device. By default, debug mode is turned off, and the port works to host mode. To be able to debug, you need to change these settings. How to do this is described in detail in /> instructions from the manufacturer.

There is one problem, there was no USB cable A - A, i.e. Dad - Dad, I had to solder two existing tails. If anyone needs to do this, then connect 1 to 1 all the wires.



After connecting the device appeared in the list of devices available for debugging.



Run!


As a TV monitor is connected to the device through the adapter HDMI-VGA. Click Run! The application has started. There are no differences from the emulator. Now you can use the remote control to navigate the application. You can also use the Android TV application to control it. What I did on my Nexus.



The mobile application of the remote can connect to Android TV via Wi-Fi or Bluetooth.



Application on Android TV.



You can publish the application in the Android Play Store! As a result, it turned out that adapting an Android TV application is not at all difficult. It is enough to add the necessary information to the application manifest and handle the movement of focus between controls. Of course, add the appropriate selector for them.

GridView and RecyclerView are able to predictably work with focus moving. If this were not enough, then you can handle the movement of the focus in the documentation itself, there is an example of how to do this.

Useful information on creating applications for TV here .

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


All Articles