有没有办法检查应用是否在Android TV或Android Mobile上运行?
我知道如何检查正在运行的构建。如果应用程序在Android TV和Mobile上运行,我想启动特定服务。希望把两个人安置在同一个图书馆里。
答案 0 :(得分:27)
来自Android文档中的Handling TV Hardware:
public static final String TAG = "DeviceTypeRuntimeCheck";
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
Log.d(TAG, "Running on a TV Device");
} else {
Log.d(TAG, "Running on a non-TV Device");
}
答案 1 :(得分:15)
private boolean isDirectToTV() {
return(getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEVISION)
|| getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK));
}
如果设备宣传自己拥有true
或android.hardware.type.television
系统功能,则会返回android.software.leanback
。 Android TV和Fire TV正确处理这个问题;我还没有在其他环境上尝试过它。
答案 2 :(得分:3)
有些选择是:
1)查询系统以获得大屏幕
2)使用反射来检测电视特定类
3)使用hasSystemFeature检测缺少触摸屏
更多信息
https://developer.android.com/training/tv/start/hardware.html
Google TV的类似答案
答案 3 :(得分:0)
假设有前言:
uiModeManager.currentModeType == UI_MODE_TYPE_TELEVISION
绝对是电视currentModeType
来表示它是电视,因为某些电视设备实际上返回了UI_MODE_TYPE_NORMAL
FEATURE_TELEVISION
是否有棒棒糖,FEATURE_LEANBACK
是否有棒棒糖设备。 这是我们实际使用的kotlin代码:
enum class UiModeType {
NORMAL,
DESK,
CAR,
TV,
APPLIANCE,
WATCH,
VR
}
private val modeType: Int
get() = uiModeManager.currentModeType
fun getUiModeType(): UiModeType = when {
modeType == UI_MODE_TYPE_APPLIANCE -> UiModeType.APPLIANCE
modeType == UI_MODE_TYPE_CAR -> UiModeType.CAR
modeType == UI_MODE_TYPE_DESK -> UiModeType.DESK
modeType == UI_MODE_TYPE_TELEVISION -> UiModeType.TV
sdkInt >= Build.VERSION_CODES.KITKAT_WATCH &&
modeType == UI_MODE_TYPE_WATCH -> UiModeType.WATCH
sdkInt >= Build.VERSION_CODES.O &&
modeType == UI_MODE_TYPE_VR_HEADSET -> UiModeType.VR
isLikelyTelevision() -> UiModeType.TV
modeType == UI_MODE_TYPE_NORMAL -> UiModeType.NORMAL
else -> UiModeType.NORMAL
}
private fun isLikelyTelevision(): Boolean = with(packageManager) {
return@with when {
sdkInt >= Build.VERSION_CODES.LOLLIPOP &&
hasSystemFeature(PackageManager.FEATURE_LEANBACK) -> true
sdkInt < Build.VERSION_CODES.LOLLIPOP &&
@Suppress("DEPRECATION")
hasSystemFeature(PackageManager.FEATURE_TELEVISION) -> true
isBatteryAbsent() &&
hasSystemFeature(PackageManager.FEATURE_USB_HOST) &&
hasSystemFeature(PackageManager.FEATURE_ETHERNET) &&
!hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)
}
@SuppressLint("NewApi")
private fun isBatteryAbsent(): Boolean {
return if (sdkInt >= Build.VERSION_CODES.LOLLIPOP) {
batteryManager?.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) == 0
} else {
false
}
}