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

วิธีเริ่มแอป Android อัตโนมัติเมื่อ Boot Complete

ภาพรวม

ตัวอย่างนี้แสดงวิธีเปิดแอปพลิเคชันอัตโนมัติเมื่ออุปกรณ์บูทเสร็จสิ้น โดยใช้ BroadcastReceiver ฟัง Event BOOT_COMPLETED

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

Android Studio → File → New Project

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World"
        android:textSize="24sp"
        android:textStyle="bold"/>

</RelativeLayout>

ขั้นตอนที่ 3: สร้าง BroadcastReceiver (StartAppOnBoot.java)

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartAppOnBoot extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

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

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

ขั้นตอนที่ 5: Manifest (androidManifest.xml)

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

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

        <receiver android:name=".StartAppOnBoot">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

    </application>
</manifest>

การทดสอบ

  1. ติดตั้งและเปิดแอปครั้งแรก (จำเป็นสำหรับ Android 3.1+)
  2. รีบูทอุปกรณ์
  3. แอปจะเปิดอัตโนมัติหลังบูทเสร็จ

ข้อจำกัดและข้อควรระวัง

  • Android 3.1+: แอปต้องถูกเปิดโดยผู้ใช้อย่างน้อย 1 ครั้งก่อน จึงจะรับ BOOT_COMPLETED ได้
  • Android 8.0+: ไม่สามารถเริ่ม Activity จาก Background ได้โดยตรง ควรใช้ Notification หรือ Foreground Service แทน
  • บาง OEM (Samsung, Xiaomi, Huawei) มีการจัดการ Auto-start แยกต่างหาก - ผู้ใช้อาจต้องอนุญาตใน Settings
  • QUICKBOOT_POWERON สำหรับอุปกรณ์บางรุ่นที่ใช้ Quick Boot

ทางเลือกสมัยใหม่

แทนที่จะเปิด Activity โดยตรง ให้พิจารณา:

  • WorkManager - สำหรับงานที่ต้องทำใน Background แบบ Deferrable
  • Foreground Service + Notification - สำหรับงานที่ต้องทำงานต่อเนื่อง
  • JobScheduler (API 21+) - สำหรับงานตามเงื่อนไข (ชาร์จ, WiFi, Idle)