如何判断Android设备是手机还是手机?

时间:2013-07-01 02:33:03

标签: android

如何判断Android设备是手机还是打击垫,我找不到Android API的某些方法。现在我根据设备尺寸判断它,if(size> 6) - > pad else --- >电话,它有另一种解决方案

3 个答案:

答案 0 :(得分:5)

我知道这不是你想听到的,但你不区分手机或平板电脑。

你需要问问自己,为什么?
- 有7英寸+设备具有电话功能。
- 有5英寸 - 没有电话功能的设备。
- 传感器因设备而异,无论大小。
- 有平板可能属于任何一种类别。

所以,如果我对“手机”的定义是“它可以拨打电话吗?”然后......

TelephonyManager manager = 
        (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE)
{ // it has no phone 
}

答案 1 :(得分:0)

此功能可以检查设备是否为平板电脑。

/**
 * Checks if the device is a tablet or a phone
 * 
 * @param activityContext
 *            The Activity Context.
 * @return Returns true if the device is a Tablet
 */
public static boolean isTabletDevice(Context activityContext) {
    // Verifies if the Generalized Size of the device is XLARGE to be
    // considered a Tablet
    boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout & 
                        Configuration.SCREENLAYOUT_SIZE_MASK) == 
                        Configuration.SCREENLAYOUT_SIZE_XLARGE);

    // If XLarge, checks if the Generalized Density is at least MDPI
    // (160dpi)
    if (xlarge) {
        DisplayMetrics metrics = new DisplayMetrics();
        Activity activity = (Activity) activityContext;
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
        // DENSITY_TV=213, DENSITY_XHIGH=320
        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) {

            // Yes, this is a tablet!
            return true;
        }
    }

    // No, this is not a tablet!
    return false;
}

答案 2 :(得分:0)

我已经找到了缩放位图的最佳方法,例如从游戏的角度来看,大概是要弄清楚您希望图像占据屏幕的多少百分比,例如,如果我有一个播放器,并且我的图像为256x256分辨率,我处于人像模式,我希望图像占据屏幕宽度的大约33%,我按屏幕宽度的那个百分比缩放图像,而不是硬编码值,那么无论您是什么屏幕,一切都会调整大小上。代码例如:

private RectF rect;
private Bitmap bitmap;
private int width;

CritterPlayer(Context context, int screenX, int screenY){
    rect = new RectF();

    //percentage of screen
    width = screenX / 3;

//load bitmap
    bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.player);

//scale bitmap
    bitmap = Bitmap.createScaledBitmap(bitmap,
            width,
            width,
            false);
//get center
    x = (screenX - bitmap.getWidth()) / 2;
    y = (screenY - bitmap.getHeight()) / 2;

}
相关问题