Android 如何检测设备是安卓手机还是安卓平板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11330363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:00:21  来源:igfitidea点击:

How to detect device is Android phone or Android tablet?

androiddevice-detection

提问by KAPLANDROID

I have two apps for Android tablets and Android phones. For tablet app I set android:minSdkVersion="11". But nowadays Android phones like Galaxy S3 has Android version 4.0.4 so S3 users can download my tablet app from Google Play Store. I want to warn phone users to download phone app when they install tablet app. Vice versa for tablet users download tablet app when they run phone app.

我有两个适用于 Android 平板电脑和 Android 手机的应用程序。对于平板电脑应用,我设置了android:minSdkVersion="11". 但是现在像 Galaxy S3 这样的 Android 手机有 Android 版本 4.0.4,所以 S3 用户可以从 Google Play 商店下载我的平板电脑应用程序。我想警告手机用户在安装平板电脑应用程序时下载手机应用程序。反之亦然,平板电脑用户在运行手机应用程序时下载平板电脑应用程序。

Is there any easy way to detect the device type?

有什么简单的方法可以检测设备类型吗?

Edit:

编辑:

I found solution on this link.

我在这个链接上找到了解决方案。

In your manifest file you can declare screen feature for handset and tablets then Google Play decides download permissions for both phone and tablet.

在您的清单文件中,您可以声明手机和平板电脑的屏幕功能,然后 Google Play 决定手机和平板电脑的下载权限。

回答by Alex Lockwood

Use this:

用这个:

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

which will return true if the device is operating on a large screen.

如果设备在大屏幕上运行,它将返回 true。

Some other helpful methods can be found here.

其他一些有用的方法可以在这里找到

回答by Bhavana Vadodariya

You can also try this
Add booleanparameter in resource file.
in res/values/dimen.xml file add this line

您也可以尝试在资源文件中
添加布尔参数。
在 res/values/dimen.xml 文件中添加这一行

<bool name="isTab">false</bool>

while in res/values-sw600dp/dimen.xml file add this line:

而在 res/values-sw600dp/dimen.xml 文件中添加这一行:

<bool name="isTab">true</bool>

then in your java file get this value:

然后在您的 java 文件中获取此值:

if(getResources().getBoolean(R.bool.isTab)) {
    System.out.println("tablet");
} else {
    System.out.println("mobile");
}

回答by Pawan Maheshwari

This code snippet will tell whether device type is 7" Inch or more and Mdpi or higher resolution. You can change the implementation as per your need.

此代码段将说明设备类型是否为 7" 英寸或更大,以及 Mdpi 或更高分辨率。您可以根据需要更改实现。

 private static boolean isTabletDevice(Context activityContext) {
        boolean device_large = ((activityContext.getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK) ==
                Configuration.SCREENLAYOUT_SIZE_LARGE);

        if (device_large) {
            DisplayMetrics metrics = new DisplayMetrics();
            Activity activity = (Activity) activityContext;
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

            if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                    || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                    || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                    || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                    || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
                AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-True");
                return true;
            }
        }
        AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-False");
        return false;
    }

回答by HopefullyHelpful

I think this should detect if something is able to make a phonecall, everything else would be a tablet/TV without phone capabilities.

我认为这应该检测是否可以拨打电话,其他一切都是没有电话功能的平板电脑/电视。

As far as I have seen this is the only thing not relying on screensize.

据我所知,这是唯一不依赖屏幕大小的东西。

public static boolean isTelephone(){
    //pseudocode, don't have the copy and paste here right know and can't remember every line
    return new Intent(ACTION_DIAL).resolveActivity() != null;
}

回答by UdayaLakmal

If you want to decide device is tablet or phone based on screen inch, you can use following

如果您想根据屏幕英寸决定设备是平板电脑还是手机,您可以使用以下方法

device 6.5 inches or higher consider as tablet, but some recent handheld phone has higher diagonal value. good thing with following solution you can set the margin.

6.5 英寸或更高的设备视为平板电脑,但最近的一些手持电话具有更高的对角线值。使用以下解决方案的好处是您可以设置边距。

public boolean isDeviceTablet(){
    DisplayMetrics metrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float yInches= metrics.heightPixels/metrics.ydpi;
    float xInches= metrics.widthPixels/metrics.xdpi;
    double diagonalInches = Math.sqrt(xInches*xInches + yInches*yInches);
    if (diagonalInches>=6.5) {
        return true;
    }
    return false;
}

回答by Marcio Covre

Use Google Play store capabilities and only enable to download your tablet app on tablets and the phone app on phones.

使用 Google Play 商店功能,只能在平板电脑上下载您的平板电脑应用程序,在手机上下载手机应用程序。

If the user then installs the wrong app then they must have installed using another method.

如果用户随后安装了错误的应用程序,则他们必须使用其他方法进行安装。

回答by user3703142

We had the similar issue with our app that should switch based on the device type - Tab/Phone. IOS gave us the device type perfectly but the same idea wasn't working with Android. the resolution/ DPI method failed with small res tabs, high res phones. after a lot of torn hairs and banging our heads over the wall, we tried a weird idea and it worked exceptionally well that won't depend on the resolutions. this should help you too.

我们的应用程序也有类似的问题,应该根据设备类型 - Tab/Phone 进行切换。IOS 完美地为我们提供了设备类型,但同样的想法不适用于 Android。分辨率/ DPI 方法因小分辨率选项卡、高分辨率手机而失败。在经历了很多撕裂的头发并将我们的头撞到墙上之后,我们尝试了一个奇怪的想法,它运行得非常好,不依赖于分辨率。这也应该对你有帮助。

in the Main class, write this and you should get your device type as null for TAB and mobile for Phone.

在 Main 类中,编写此内容,您应该将 TAB 的设备类型设置为 null,电话的移动设备类型设置为 null。

String ua=new WebView(this).getSettings().getUserAgentString();


if(ua.contains("Mobile")){
   //Your code for Mobile
}else{
   //Your code for TAB            
}

回答by Vaishali Sutariya

Use following code to identify device type.

使用以下代码来识别设备类型。

private boolean isTabletDevice() {
     if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
         // test screen size, use reflection because isLayoutSizeAtLeast is only available since 11
         Configuration con = getResources().getConfiguration();
         try {
              Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast");
              Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
              return r;
         } catch (Exception x) {
              return false;
         }
      }
    return false;
}