下面列出了android.content.res.Configuration#UI_MODE_TYPE_TELEVISION 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public DeviceType getDeviceType() {
// Detect TVs via ui mode (Android TVs) or system features (Fire TV).
if (context.getPackageManager().hasSystemFeature("amazon.hardware.fire_tv")) {
return DeviceType.TV;
}
UiModeManager uiManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
if (uiManager != null && uiManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
return DeviceType.TV;
}
DeviceType deviceTypeFromConfig = getDeviceTypeFromResourceConfiguration();
if (deviceTypeFromConfig != null && deviceTypeFromConfig != DeviceType.UNKNOWN) {
return deviceTypeFromConfig;
}
return getDeviceTypeFromPhysicalSize();
}
/**
* Determines if the device uses navigation controls as the primary navigation from a number of factors.
* @param context Application Context
* @return True if the device uses navigation controls, false otherwise.
*/
public static boolean usesNavigationControl(Context context) {
Configuration configuration = context.getResources().getConfiguration();
if (configuration.navigation == Configuration.NAVIGATION_NONAV) {
return false;
} else if (configuration.touchscreen == Configuration.TOUCHSCREEN_FINGER) {
return false;
} else if (configuration.navigation == Configuration.NAVIGATION_DPAD) {
return true;
} else if (configuration.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH) {
return true;
} else if (configuration.touchscreen == Configuration.TOUCHSCREEN_UNDEFINED) {
return true;
} else if (configuration.navigationHidden == Configuration.NAVIGATIONHIDDEN_YES) {
return true;
} else if (configuration.uiMode == Configuration.UI_MODE_TYPE_TELEVISION) {
return true;
}
return false;
}
/**
* Returns true when running Android TV
*
* @param c Context to detect UI Mode.
* @return true when device is running in tv mode, false otherwise.
*/
public static String getDeviceType(Context c) {
UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
int modeType = uiModeManager.getCurrentModeType();
switch (modeType){
case Configuration.UI_MODE_TYPE_TELEVISION:
return "TELEVISION";
case Configuration.UI_MODE_TYPE_WATCH:
return "WATCH";
case Configuration.UI_MODE_TYPE_NORMAL:
String type = isTablet(c) ? "TABLET" : "PHONE";
return type;
case Configuration.UI_MODE_TYPE_UNDEFINED:
return "UNKOWN";
default:
return "";
}
}
/**
* Returns true when running Android TV
*
* @param c Context to detect UI Mode.
* @return true when device is running in tv mode, false otherwise.
*/
public static String getDeviceType(Context c) {
UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
int modeType = uiModeManager.getCurrentModeType();
switch (modeType){
case Configuration.UI_MODE_TYPE_TELEVISION:
return "TELEVISION";
case Configuration.UI_MODE_TYPE_WATCH:
return "WATCH";
case Configuration.UI_MODE_TYPE_NORMAL:
String type = isTablet(c) ? "TABLET" : "PHONE";
return type;
case Configuration.UI_MODE_TYPE_UNDEFINED:
return "UNKOWN";
default:
return "";
}
}
/**
* Returns whether the app is running on a TV device.
*
* @param context Any context.
* @return Whether the app is running on a TV device.
*/
public static boolean isTv(Context context) {
// See https://developer.android.com/training/tv/start/hardware.html#runtime-check.
UiModeManager uiModeManager =
(UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE);
return uiModeManager != null
&& uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
private void start(String host, int port) {
Log.d(TAG, "start host: " + host + ", port: " + port);
try {
//https://code.google.com/p/android/issues/detail?id=22763
if (running)
return;
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
Log.d(TAG, "Running on a TV Device");
wakeLock = powerManager.newWakeLock(FULL_WAKE_LOCK, "snapcast:SnapcastFullWakeLock");
} else {
Log.d(TAG, "Running on a non-TV Device");
wakeLock = powerManager.newWakeLock(PARTIAL_WAKE_LOCK, "snapcast:SnapcastPartialWakeLock");
}
wakeLock.acquire();
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
wifiWakeLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "snapcast:SnapcastWifiWakeLock");
wifiWakeLock.acquire();
this.host = host;
this.port = port;
startProcess();
} catch (Exception e) {
e.printStackTrace();
if (listener != null)
listener.onError(this, e.getMessage(), e);
stop();
}
}
public static boolean isTv(Context context) {
if (context == null) {
return false;
}
UiModeManager modeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
return ((modeManager != null)
&& (modeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION));
}
/**
* This method is called by SDL using JNI.
*/
public static boolean isAndroidTV() {
UiModeManager uiModeManager = (UiModeManager) getContext().getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
return true;
}
if (Build.MANUFACTURER.equals("MINIX") && Build.MODEL.equals("NEO-U1")) {
return true;
}
if (Build.MANUFACTURER.equals("Amlogic") && Build.MODEL.equals("X96-W")) {
return true;
}
return false;
}
/**
* Returns whether the app is running on a TV device.
*
* @param context Any context.
* @return Whether the app is running on a TV device.
*/
public static boolean isTv(Context context) {
// See https://developer.android.com/training/tv/start/hardware.html#runtime-check.
UiModeManager uiModeManager =
(UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE);
return uiModeManager != null
&& uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
private boolean runningOnAndroidTV() {
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
@Override
protected void onCreate(Bundle bundle) {
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor
(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
// tv = true;
}
PackageManager pm = getPackageManager();
if(!pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)) {
touchscreen = false;
}
setUncaughtExceptionHandler();
applyTheme();
applyFullscreen();
super.onCreate(bundle);
DownloadService.startService(this);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
if(getIntent().hasExtra(Constants.FRAGMENT_POSITION)) {
lastSelectedPosition = getIntent().getIntExtra(Constants.FRAGMENT_POSITION, 0);
}
if(preferencesListener == null) {
preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// When changing drawer settings change visibility
switch(key) {
case Constants.PREFERENCES_KEY_PODCASTS_ENABLED:
setDrawerItemVisible(R.id.drawer_podcasts, false);
break;
case Constants.PREFERENCES_KEY_BOOKMARKS_ENABLED:
setDrawerItemVisible(R.id.drawer_bookmarks, false);
break;
case Constants.PREFERENCES_KEY_ADMIN_ENABLED:
setDrawerItemVisible(R.id.drawer_admin, false);
break;
}
}
};
Util.getPreferences(this).registerOnSharedPreferenceChangeListener(preferencesListener);
}
RateThisApp.Config config = new RateThisApp.Config(30, 150);
RateThisApp.init(config);
RateThisApp.onCreate(this);
RateThisApp.showRateDialogIfNeeded(this);
}
public static boolean isTV(@NonNull Context context) {
UiModeManager uiModeManager =
(UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
private boolean runningOnAndroidTV() {
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
public static boolean isTV(Context context) {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2 && ((UiModeManager) context
.getSystemService(Context.UI_MODE_SERVICE))
.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
public static boolean isTv(Context context) {
return ((UiModeManager) Objects.requireNonNull(context.getSystemService(Context.UI_MODE_SERVICE)))
.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
private boolean runningOnAndroidTV() {
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
public static boolean isTV(Context context) {
return ((UiModeManager) context
.getSystemService(Context.UI_MODE_SERVICE))
.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
public static boolean isTv(Context context) {
return ((UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE))
.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
public static boolean isTelevision(Context context) {
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
/**
* 检测是否在Android TV环境下
*
* @param context {@link PackageManager}
* @return boolean
* @see <a href="https://developer.android.com/training/tv/start/hardware.html#check-features">处理 TV 硬件 - 检查硬件功能</a>
*/
public static boolean isAndroidTV(Context context) {
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}