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

วิธีปิดใช้งานปุ่ม Home บนอุปกรณ์ Android (Kiosk Mode)

ตัวอย่างนี้สาธิตวิธีป้องกันไม่ให้ผู้ใช้กดปุ่ม Home ออกจากแอปได้ โดยใช้ ActivityManager.moveTaskToFront() และตั้งค่า Category HOME ใน Manifest (Kiosk Mode / Launcher Mode)

⚠️ ข้อควรระวังสำคัญ

การใช้งานจริงต้องระวัง:

  • ต้องการ Permission REORDER_TASKS (Signature|Privileged - แอปทั่วไปไม่ได้)
  • Android 5.0+ มี Screen Pinning และ Lock Task Mode เป็นทางเลือกที่ดีกว่า
  • การ Override Home Button ถูกจำกัดมากขึ้นใน Android ใหม่ๆ
  • วิธีนี้เหมาะสำหรับ Enterprise/Device Owner App เท่านั้น

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

Android Studio → File → New Project

ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    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:text="Kiosk Mode Demo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ขั้นตอนที่ 3: MainActivity.java

package com.medkart.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onPause() {
        super.onPause();
        
        // พยายามดึงแอปกลับมาหน้าจอเมื่อถูกส่งไปเบื้องหลัง
        ActivityManager activityManager = (ActivityManager) getApplicationContext()
                .getSystemService(Context.ACTIVITY_SERVICE);
        
        if (activityManager != null) {
            activityManager.moveTaskToFront(getTaskId(), 0);
        }
    }
}

ขั้นตอนที่ 4: AndroidManifest.xml (สำคัญมาก)

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

    <!-- Permission ที่ต้องการ (แอปทั่วไปไม่ได้รับ) -->
    <uses-permission android:name="android.permission.REORDER_TASKS" />

    <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" />
                <!-- สำคัญ: ทำให้แอปเป็น Home Launcher -->
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>
</manifest>

วิธีการทำงาน

  1. เพิ่ม android.intent.category.HOME และ DEFAULT ใน Intent Filter → แอปจะถูกมองว่าเป็น Launcher
  2. เมื่อกดปุ่ม Home → ระบบจะแสดง Dialog เลือก Launcher (รวมถึงแอปนี้)
  3. ใน onPause() ใช้ moveTaskToFront() พยายามดึงแอปกลับมาด้านหน้า

ทางเลือกที่ดีกว่าสำหรับ Kiosk Mode (แนะนำ)

1. Screen Pinning (Android 5.0+)

// เริ่ม Pin
startLockTask();

// หยุด Pin (ต้องเป็น Device Owner หรือ User อนุญาต)
stopLockTask();

ต้องเปิดใน Settings → Security → Screen Pinning

2. Lock Task Mode (Android 5.0+ Device Owner)

// ใน DevicePolicyController
DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName admin = new ComponentName(this, AdminReceiver.class);
String[] packages = {getPackageName()};
dpm.setLockTaskPackages(admin, packages);

// เริ่ม
startLockTask();

3. Android Management API / Enterprise Mobility Management (EMM)

สำหรับองค์กรที่จัดการอุปกรณ์จำนวนมาก

การทดสอบ

  1. Install แอป
  2. กดปุ่ม Home → จะขึ้น Dialog เลือก Launcher
  3. เลือกแอปนี้ → จะอยู่ในโหมด Kiosk
  4. กด Home อีกครั้ง → onPause ทำงาน → พยายามกลับมา

หมายเหตุ: บน Android ใหม่ (10+) วิธีนี้ทำงานไม่ได้เต็มที่ ต้องใช้ Lock Task Mode หรือ Device Owner แทน