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

วิธีตรวจสอบว่าแอป Android ทำงานใน Background หรือไม่ด้วย Kotlin

บทนำ

ตัวอย่างนี้สาธิตวิธีตรวจสอบว่าแอปพลิเคชัน Android กำลังทำงานในพื้นหลัง (Background) หรือไม่ โดยใช้ ActivityManager และ RunningAppProcessInfo ใน Kotlin

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

สร้างโปรเจกต์ใหม่ใน Android Studio

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

เพิ่มโค้ดใน res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Checking if Android Application is running in Background"
        android:textAlignment="center"
        android:textColor="@android:color/holo_red_light"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

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

เพิ่มโค้ดใน src/MainActivity.kt:

import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    var appRunningBackground: Boolean = false

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

        val runningAppProcessInfo = RunningAppProcessInfo()
        ActivityManager.getMyMemoryState(runningAppProcessInfo)
        appRunningBackground = runningAppProcessInfo.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND

        if (appRunningBackground) {
            Toast.makeText(applicationContext, "Your Android Application is Running in Background", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(applicationContext, "Your Android Application is not Running in Background", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onPause() {
        super.onPause()
        Toast.makeText(applicationContext, "Your Android Application is Running in Background", Toast.LENGTH_SHORT).show()
    }
}

หมายเหตุ: วิธีนี้ใช้ getMyMemoryState ซึ่งอาจไม่แม่นยำใน Android เวอร์ชันใหม่ (API 26+) ที่มีการจัดการ Background Execution Limits แนะนำให้ใช้ ProcessLifecycleOwner จาก androidx.lifecycle:lifecycle-process แทนสำหรับการตรวจสอบสถานะ Foreground/Background อย่างถูกต้อง

ขั้นตอนที่ 4: AndroidManifest.xml

ตรวจสอบการประกาศ Activity

การทดสอบ

รันแอป กดปุ่ม Home เพื่อส่งแอปไปพื้นหลัง จะแสดง Toast ว่าอยู่ใน Background

วิธีตรวจสอบว่าแอป Android ทำงานใน Background หรือไม่ด้วย Kotlin วิธีตรวจสอบว่าแอป Android ทำงานใน Background หรือไม่ด้วย Kotlin