正确检测Android设备类型

Spo*_*ght 25 android

有没有办法精确检测设备类型(手机,平板电脑,手表,电视,汽车,PC)?

现在,我找到了一种方法来检测应用程序是在汽车(uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR),电视(uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION)或手表(uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_WATCH)上运行.这是对的吗?连接到汽车的手机是显示为手机还是"Android Auto"?

为了区分手机,平板电脑或计算机,我可以检查最小屏幕尺寸(600dp,例如平板电脑或笔记本电脑).

现在的问题是区分平板电脑和笔记本电脑.你有什么想法吗?

PS:我不是要求它制作响应式用户界面,这是一个与帐户设备管理相关的问题

MIk*_*mik 11

您可以使用在大屏幕上运行的此代码应用程序进行检测.

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
  }
Run Code Online (Sandbox Code Playgroud)

这个链接对你也有帮助.

获取屏幕宽度并使用此断点检查.

/*平板电脑(肖像和风景)-----------*/

 min-device-width : 768px
 max-device-width : 1024px
Run Code Online (Sandbox Code Playgroud)

/*台式机和笔记本电脑-----------*/

min-width : 1224px
Run Code Online (Sandbox Code Playgroud)

  • 正如之前所指出的,像素数并不是确定设备类型的可靠方法,因为许多新发布的手机的尺寸远远超过几年前普通平板电脑的尺寸。 (2认同)

A. *_*han 5

为了区分手机和平板电脑或计算机,我可以检查最小屏幕尺寸(例如600dp就可以作为服务员或笔记本电脑).

有一种更好的方法可以做到这一点,它正在使用价值观.例如,如果您有两种类型的设备(例如手机和平板电脑),也可以为值创建两个文件夹.然后为values文件夹添加:

<resources>
    <bool name="isLarge">false</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

并在您的值 - 大文件夹中:

<resources>
    <bool name="isLarge">true</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后在你的活动中:

boolean isLarge = getResources().getBoolean(R.bool.isLarge);
if (isLarge) {
    // do something
} else {
    // do something else
}
Run Code Online (Sandbox Code Playgroud)

使用这个,你可以为手机,sw-600dp,sw-720dp等做同样的事情.我不确定你是否可以将它用于电视等,但我认为值得尝试.


Jij*_*ijo 5

请参考此链接,

http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali

下面我放了检查平板电脑或安卓电视的代码,请检查,它会起作用

用于平板电脑

private boolean checkIsTablet() {
   boolean isTablet;
   Display display = ((Activity)   this.mContext).getWindowManager().getDefaultDisplay();
   DisplayMetrics metrics = new DisplayMetrics();
   display.getMetrics(metrics);

  float widthInches = metrics.widthPixels / metrics.xdpi;
  float heightInches = metrics.heightPixels / metrics.ydpi;
  double diagonalInches = Math.sqrt(Math.pow(widthInches, 2) + Math.pow(heightInches, 2));
  if (diagonalInches >= 7.0) {
    isTablet = true;
  }

return isTablet;
}
Run Code Online (Sandbox Code Playgroud)

或者

public static boolean checkIsTablet(Context ctx){    
return (ctx.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; 
}
Run Code Online (Sandbox Code Playgroud)

电视用

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private boolean checkIsTelevision() {
  boolean isAndroidTV;
  int uiMode = mContext.getResources().getConfiguration().uiMode;
  if ((uiMode & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_TELEVISION) {
    isAndroidTV = true;
}
Run Code Online (Sandbox Code Playgroud)

它将有效,享受...


Raj*_*Raj 0

检查这个类。https://developer.android.com/reference/android/os/Build.html

String manufacturer = Build.MANUFACTURER
String model = Build.MODEL
Run Code Online (Sandbox Code Playgroud)

您可以通过 Build.MODEL 知道它是手机、平板电脑还是电视

  • 不幸的是,这种方法不适用于这种情况,因为我需要手动将每个设备型号与其类型关联起来。我需要一种今天有效、明天也有效的方法...我想它不存在:( (2认同)