{"id":602,"date":"2017-08-09T00:15:38","date_gmt":"2017-08-08T23:15:38","guid":{"rendered":"http:\/\/www.pixnbgames.com\/blog\/?p=602"},"modified":"2017-08-09T00:15:38","modified_gmt":"2017-08-08T23:15:38","slug":"how-to-know-if-my-android-app-is-running-on-a-television-or-on-a-mobile-device","status":"publish","type":"post","link":"https:\/\/www.pixnbgames.com\/blog\/libgdx\/how-to-know-if-my-android-app-is-running-on-a-television-or-on-a-mobile-device\/","title":{"rendered":"How to know if my Android app is running on a television or on a mobile device"},"content":{"rendered":"<p>Given that Android apps can run on different devices, there are situations where we must know if the device where the app is running on is of type smartphone or television. This allows our app to adapt itself to the running device without the must of generate different app versions for each device type.<\/p>\n<p>The way to know it in Android is very simple, using the method <code>getCurrentModeType()<\/code> of the class <code>android.app.UiModeManager<\/code>.<\/p>\n<h2>Using it in libGDX<\/h2>\n<p>The way I use this in libGDX is by declaring an <code>enum<\/code> that defines the supported game modes.<\/p>\n<pre lang=\"Java\" line=\"1\">\/** List of supported game modes. *\/\r\npublic static enum GameMode {\r\n    \/** With touch screen: smartphones, tablets, etc. *\/\r\n    PHONE,\r\n\t\r\n    \/** Without touch screen: televisions. *\/\r\n    TV\r\n}\r\n<\/pre>\n<p>On the other hand, in the game <i>core<\/i> I have a configuration class, with different constants that define the game behavior. One of this values is the <code>gameMode<\/code>:<\/p>\n<pre lang=\"Java\">\/** Game configuration. *\/\r\npublic class GameConfig {\r\n\r\n    \/** Game mode: TV by default. *\/\r\n    public static GameMode gameMode = GameMode.TV;\r\n    ...\r\n}\r\n<\/pre>\n<p>From the Android launcher of the game, class <code>AndroidLauncher<\/code>, it&#8217;s setted the value of the variable <code>gameMode<\/code>:<\/p>\n<pre lang=\"Java\">public class AndroidLauncher extends AndroidApplication {\r\n    @Override\r\n    protected void onCreate (Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n\r\n        \/\/ Determines if the hardware is a television or not\r\n        UiModeManager uiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);\r\n        if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {\r\n            GameConfig.gameMode = GameMode.TV;\r\n        } else {\r\n            GameConfig.gameMode = GameMode.PHONE;\r\n        }\r\n        \r\n        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();\r\n        initialize(new MyGdxGame(), config);\r\n    }\r\n    ....\r\n}\r\n<\/pre>\n<p>That&#8217;s all. Now, we can check anytime the stablished game mode and do the needed actions for each case:<\/p>\n<pre lang=\"Java\">...\r\nif (GameConfig.gameMode == GameMode.PHONE) {\r\n    \/\/ do ...\r\n} else {\r\n    \/\/ do ...\r\n}\r\n...\r\n<\/pre>\n<p>Regards!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given that Android apps can run on different devices, there are situations where we must know if the device where the app is running on is of type smartphone or television. This allows our app to adapt itself to the <a class=\"more-link\" href=\"https:\/\/www.pixnbgames.com\/blog\/libgdx\/how-to-know-if-my-android-app-is-running-on-a-television-or-on-a-mobile-device\/\">Seguir leyendo <span class=\"screen-reader-text\">  How to know if my Android app is running on a television or on a mobile device<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[19,14],"tags":[20,39,15],"language":[4],"_links":{"self":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/602"}],"collection":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/comments?post=602"}],"version-history":[{"count":8,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/602\/revisions"}],"predecessor-version":[{"id":610,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/602\/revisions\/610"}],"wp:attachment":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/media?parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/categories?post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/tags?post=602"},{"taxonomy":"language","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/language?post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}