Android 判斷當前裝置是手機還是平板,android平板
Android開發需要適配手機和平板,有些需求實現時就要求判斷裝置是手機還是平板。
網上很多說通過裝置尺寸、DPI、版本號碼、是否具備電話功能等進行判斷,不過都不算太精確。
這裡分享一個簡潔給力的方法(官方用法):
/** * 判斷當前裝置是手機還是平板,代碼來自 Google I/O App for Android * @param context * @return 平板返回 True,手機返回 False */public static boolean isPad(Context context) { return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;}
令附上是否具備電話功能判斷方法(現在部分平板也可以打電話):
public static boolean isPad(Activity activity) { TelephonyManager telephony = (TelephonyManager)activity.getSystemService(Context.TELEPHONY_SERVICE); if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) { return true; }else { return false; }}
至於裝置尺寸、解析度、版本號碼,以目前手機來看,個人認為精度太差了,不建議使用。
其他精彩文章文章
android學習筆記(41)android選項菜單和子功能表(SubMenu )android學習筆記(40)Notification的功能與用法android學習筆記(42)android使用監聽器來監聽菜單事件android學習筆記(43)android建立單選菜單和複選菜單 jQuery教程(12)-ajax操作之基於請求載入資料 jQuery教程(13)-ajax操作之追加 HTML