开发一个通用的Andr​​oid应用程序(手机和平板)平板、应用程序、手机、Andr

2023-09-05 23:30:33 作者:口吻生花

我开发一个通用的Andr​​oid应用程序,我需要检查,如果应用程序在平板电脑或手机上运行。有没有什么方法来做到这一点?

I'm developing an universal android application and I need to check if the application is running in a tablet or in a phone. Is there any method to do it?

推荐答案

您可以查看谷歌I / O应用程序对于Android SRC code:

You can check the Google I/O App for Android src code:

在UIUtils类,你有以下几种方法:

In the UIUtils class you have the following methods:

public static boolean isHoneycomb() {
    // Can use static final constants like HONEYCOMB, declared in later versions
    // of the OS since they are inlined at compile time. This is guaranteed behavior.
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}

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

public static boolean isHoneycombTablet(Context context) {
    return isHoneycomb() && isTablet(context);
}