10

Android 应用程序有没有办法在 Java 代码中判断它是在 GoogleTV 上运行还是在平板电脑或手机上运行?

4

4 回答 4

10

您可以询问包管理器:

/**
 * Test if this device is a Google TV.
 * 
 * See 32:00 in "Google I/O 2011: Building Android Apps for Google TV"
 * http://www.youtube.com/watch?v=CxLL-sR6XfM
 * 
 * @return true if google tv
 */
public static boolean isGoogleTV(Context context) {
    final PackageManager pm = context.getPackageManager();
    return pm.hasSystemFeature("com.google.android.tv");
}

加上这个清单行:

<uses-feature android:name="com.google.android.tv" android:required="false" />
于 2011-07-20T19:24:37.173 回答
6

根据官方文档

确定您的应用程序是否在电视设备上运行的推荐方法是使用 UiModeManager.getCurrentModeType() 方法检查设备是否在电视模式下运行。以下示例代码向您展示了如何检查您的应用程序是否在电视设备上运行:

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");
}
于 2015-08-13T13:56:32.287 回答
5

以下链接可能对您有所帮助:Google TV Android 开发者指南要针对 Google TV 优化您的应用程序,只需为大屏幕添加额外的层。但是,如果您想在运行时确定当前正在使用该应用程序的设备,您可以尝试使用hasSystemFeature()方法。有了这个,您可以测试某些特定于 Google TV 的硬件功能(例如,您可以测试 FEATURE_TOUCHSCREEN,因为除了 Google TV 之外的任何设备都有一个 <=> 如果该功能不受支持,则该应用程序可能正在电视上运行) .

于 2011-06-02T00:41:27.160 回答
1

以下是我为反馈收集有用信息的方式。我不知道是否可以检测设备的类型(电话、桌子、Google TV),但可以构建某种映射数据库并匹配信息

private String getDeviceInfo() {
    final StringBuilder sb = new StringBuilder("\n\n---\n");
    try {
        sb.append("Version: ").append(getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName)
                .append('\n');
    } catch (final NameNotFoundException e) {
        // Shouldn't happen but if did - ignore
        Log.e(TAG, "failed to get app version", e);
    }
    sb.append("Model: ").append(Build.MODEL).append('\n');
    sb.append("Brand: ").append(Build.BRAND).append('\n');
    sb.append("Device: ").append(Build.DEVICE).append('\n');
    sb.append("Display: ").append(Build.DISPLAY).append('\n');
    sb.append("Hardware: ").append(Build.HARDWARE).append('\n');
    sb.append("Manufacturer: ").append(Build.MANUFACTURER).append('\n');
    sb.append("Host: ").append(Build.HOST).append('\n');
    sb.append("Release: ").append(Build.VERSION.RELEASE).append('\n');
    sb.append("Board: ").append(Build.BOARD).append('\n');
    sb.append("Radio: ").append(Build.RADIO).append('\n');
    sb.append("Product: ").append(Build.PRODUCT).append('\n');
    return sb.toString();
}
于 2011-06-02T00:42:19.317 回答