Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Phát hiện thiết bị là Tablet hay Phone trong Android bằng Kotlin

Giới thiệu

Ví dụ này minh họa cách xác định thiết bị đang chạy ứng dụng là máy tính bảng (Tablet) hay điện thoại (Phone) bằng cách kiểm tra TelephonyManager trong Android Kotlin.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project, điền thông tin.

Bước 2: Giao diện (res/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"
    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="Detect device type..."
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

Bước 3: MainActivity.kt

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 isTablet = telephonyManager.phoneType == TelephonyManager.PHONE_TYPE_NONE
        val message = if (isTablet) "Detected: Tablet" else "Detected: Mobile Phone"

        textView.text = message
        Toast.makeText(this, message, Toast.LENGTH_LONG).show()
    }
}

Bước 4: AndroidManifest.xml

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

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <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>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Ứng dụng sẽ hiển thị Toast và TextView cho biết thiết bị là Tablet hay Phone.

Lưu ý:

  • Cách này dựa trên khả năng gọi điện thoại. Một số tablet có SIM vẫn báo PHONE_TYPE_NONE? Thực tế PHONE_TYPE_NONE nghĩa là không hỗ trợ thoại.
  • Cách tốt hơn: kiểm tra kích thước màn hình (sw600dp) hoặc dùng UiModeManager.
  • Từ Android 12, quyền READ_PHONE_STATE bị hạn chế, cân nhắc dùng PackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY).