Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีตรวจสอบว่าอุปกรณ์เป็นโทรศัพท์หรือแท็บเล็ตใน Android

ภาพรวม

ตัวอย่างนี้แสดงวิธีการตรวจสอบประเภทอุปกรณ์ (Phone หรือ Tablet) โดยใช้ TelephonyManager ตรวจสอบความสามารถด้านโทรศัพท์

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Detect device is Android phone or Android tablet"
        android:textAlignment="center"
        android:textSize="24sp"
        android:textStyle="bold" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ดหลัก (MainActivity.java)

package com.example.devicetypedetector;

import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        detectDeviceType();
    }
    
    private void detectDeviceType() {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        
        if (telephonyManager != null) {
            int phoneType = telephonyManager.getPhoneType();
            
            if (phoneType == TelephonyManager.PHONE_TYPE_NONE) {
                Toast.makeText(this, "ตรวจพบ: คุณกำลังใช้แท็บเล็ต (Tablet)", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "ตรวจพบ: คุณกำลังใช้โทรศัพท์มือถือ (Mobile Phone)", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

ขั้นตอนที่ 4: ตั้งค่า Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.devicetypedetector">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

การทดสอบ

รันแอปพลิเคชันบนอุปกรณ์จริง จะแสดง Toast บอกว่าเป็น Phone หรือ Tablet

วิธีตรวจสอบว่าอุปกรณ์เป็นโทรศัพท์หรือแท็บเล็ตใน Android

วิธีอื่นที่แม่นยำกว่า

1. ใช้ Configuration (Screen Size)

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

2. ใช้ Display Metrics (หน้าจอขนาดใหญ่)

public boolean isTabletBySize(Context context) {
    DisplayMetrics metrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
    double widthInches = metrics.widthPixels / metrics.xdpi;
    double heightInches = metrics.heightPixels / metrics.ydpi;
    double diagonalInches = Math.sqrt(widthInches * widthInches + heightInches * heightInches);
    
    return diagonalInches >= 7.0; // 7 นิ้วขึ้นไปถือว่า Tablet
}

3. ใช้ UiModeManager (TV, Watch, Car)

UiModeManager uiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
switch (uiModeManager.getCurrentModeType()) {
    case Configuration.UI_MODE_TYPE_TELEVISION: // Android TV
    case Configuration.UI_MODE_TYPE_WATCH:      // Wear OS
    case Configuration.UI_MODE_TYPE_CAR:        // Android Auto
}

สรุป

  • TelephonyManager: ตรวจสอบฮาร์ดแวร์โทรศัพท์ (แม่นยำสำหรับการโทร)
  • Screen Size: ตรวจสอบขนาดหน้าจอ (เหมาะสำหรับ UI Layout)
  • Display Metrics: คำนวณขนาดจริงนิ้ว (แม่นยำที่สุด)
  • แท็บเล็ตบางเครื่องมี SIM (LTE Tablet) จะถูกจับว่าเป็น PHONE_TYPE_GSM/CDMA