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

วิธีตรวจสอบว่าอุปกรณ์เป็น Tablet หรือ Phone ใน Android ด้วย Kotlin

ภาพรวม

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

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

เปิด Android Studio เลือก File → New Project

ขั้นตอนที่ 2: Layout (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"
    android:padding="16dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Tutorials Point"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="150dp"
        android:text="Detecting device type..."
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

ขั้นตอนที่ 3: MainActivity.kt (วิธี TelephonyManager)

import android.content.Context
import android.os.Bundle
import android.telephony.TelephonyManager
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        val textView = findViewById<TextView>(R.id.textView)
        val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

        val isPhone = telephonyManager.phoneType != TelephonyManager.PHONE_TYPE_NONE
        val deviceType = if (isPhone) "Mobile Phone" else "Tablet"

        val message = "Detected: You're using a $deviceType"
        textView.text = message
        Toast.makeText(this, message, Toast.LENGTH_LONG).show()
    }
}

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

1. ใช้ Configuration (Screen Size)

import android.content.res.Configuration

fun isTablet(context: Context): Boolean {
    return (context.resources.configuration.screenLayout 
        and Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
}

2. ใช้ WindowManager (ขนาดหน้าจอจริง)

import android.util.DisplayMetrics
import android.view.WindowManager

fun isTablet(context: Context): Boolean {
    val metrics = DisplayMetrics()
    (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager)
        .defaultDisplay.getMetrics(metrics)
    
    val widthInches = metrics.widthPixels / metrics.xdpi
    val heightInches = metrics.heightPixels / metrics.ydpi
    val diagonalInches = Math.sqrt(widthInches * widthInches + heightInches * heightInches)
    
    return diagonalInches >= 7.0 // 7 นิ้วขึ้นไปถือว่า Tablet
}

3. ใช้ Jetpack WindowManager (สมัยใหม่ที่สุด)

// build.gradle
implementation "androidx.window:window:1.2.0"

// ในโค้ด
import androidx.window.layout.WindowInfoTracker
import androidx.window.layout.WindowLayoutInfo
import kotlinx.coroutines.flow.collect

lifecycleScope.launch {
    WindowInfoTracker.getOrCreate(this@MainActivity)
        .windowLayoutInfo(this@MainActivity)
        .collect { layoutInfo ->
            val isFoldable = layoutInfo.displayFeatures.any { it.type == DisplayFeature.Type.FOLD }
            val isTablet = // logic ตามขนาดจอ
        }
}

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.devicetype">
    <application ... >
        <activity android:name=".MainActivity"> ... </activity>
    </application>
</manifest>

หมายเหตุสำคัญ

  • TelephonyManager: แยกได้เฉพาะ "มีโมดูลโทรศัพท์" vs "ไม่มี" - แท็บเล็ตบางรุ่นมี SIM แต่ก็ยังเป็นแท็บเล็ต
  • Screen Size: วิธีที่แนะนำโดย Google ใช้ Configuration.SCREENLAYOUT_SIZE_LARGE ขึ้นไป
  • Foldable/Flip: อุปกรณ์ใหม่ๆ ต้องใช้ WindowManager API
  • ควรใช้หลายวิธีร่วมกันเพื่อความแม่นยำสูงสุด

การทดสอบ

รันบนโทรศัพท์ จะแสดง "Mobile Phone" รันบนแท็บเล็ต จะแสดง "Tablet"