在Java代码中,Android应用程序是否可以判断自己运行在Google TV上还是平板电脑或手机上?
你可以向包管理器提出问题:
/**
* 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" />
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");
}
以下是我收集反馈有用信息的方法。我不确定是否能检测设备类型(电话、平板电脑、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();
}