ภาพรวม
ตัวอย่างนี้สาธิตวิธีตรวจสอบสถานะ Airplane Mode (โหมดเครื่องบิน) ของอุปกรณ์ Android โดยใช้ Settings.Global.getInt() และ BroadcastReceiver เพื่อฟังการเปลี่ยนแปลง
ขั้นตอนการสร้างโปรเจกต์
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project → Empty Activity
ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:gravity="center"
android:padding="16dp" />
</LinearLayout>
ขั้นตอนที่ 3: โค้ดหลัก (src/MainActivity.java)
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView textView;
private BroadcastReceiver airplaneModeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
// ตรวจสอบสถานะปัจจุบัน
updateAirplaneModeStatus();
// ลงทะเบียน Receiver เพื่อฟังการเปลี่ยนแปลง
airplaneModeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
updateAirplaneModeStatus();
Toast.makeText(context, "Airplane Mode เปลี่ยนแปลง", Toast.LENGTH_SHORT).show();
}
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(airplaneModeReceiver, filter);
}
private void updateAirplaneModeStatus() {
// วิธีที่ 1: Settings.Global (API 17+)
boolean isAirplaneModeOn = Settings.Global.getInt(
getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON,
0
) == 1;
// วิธีที่ 2: Settings.System (Deprecated API 17+)
// boolean isAirplaneModeOn = Settings.System.getInt(getContentResolver(),
// Settings.System.AIRPLANE_MODE_ON, 0) == 1;
String status = isAirplaneModeOn ? "เปิด (ON)" : "ปิด (OFF)";
textView.setText("Airplane Mode: " + status);
}
@Override
protected void onDestroy() {
super.onDestroy();
// ยกเลิก Receiver เพื่อป้องกัน Memory Leak
if (airplaneModeReceiver != null) {
unregisterReceiver(airplaneModeReceiver);
}
}
}
สองวิธีตรวจสอบ Airplane Mode
| วิธี | คำอธิบาย | API Level |
|---|---|---|
Settings.Global.getInt() | วิธีมาตรฐาน ปัจจุบัน | 17+ (Android 4.2+) |
Settings.System.getInt() | Deprecated | < 17 |
Broadcast Receiver สำหรับ Real-time Updates
- Action:
Intent.ACTION_AIRPLANE_MODE_CHANGED - Intent extras:
state(boolean) - true = เปิด, false = ปิด - เป็น Sticky Broadcast - ได้รับค่าล่าสุดทันทีเมื่อ Register
- ไม่ต้องการ Permission 特殊
ตัวอย่างการอ่านค่า State จาก Intent
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
boolean state = intent.getBooleanExtra("state", false);
String msg = state ? "Airplane Mode เปิด" : "Airplane Mode ปิด";
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
updateAirplaneModeStatus();
}
}
สิ่งที่ Airplane Mode ปิดการทำงาน
- Cellular Radio (Voice, SMS, Mobile Data)
- Wi-Fi (โดยค่าเริ่มต้น แต่ผู้ใช้เปิดได้แยกต่างหาก)
- Bluetooth (โดยค่าเริ่มต้น แต่ผู้ใช้เปิดได้แยกต่างหาก)
- NFC
- GPS (บางอุปกรณ์)
ข้อควรระวัง
- แอปไม่สามารถเปิด/ปิด Airplane Mode ให้ได้ (Security restriction) - ต้องนำทางผู้ใช้ไป Settings
Settings.Globalต้องการandroid.permission.WRITE_SETTINGSสำหรับการเขียน (แต่อ่านไม่ต้อง)- บาง OEM อาจมีการทำงานแตกต่าง (Samsung, Xiaomi, etc.)
- ใน Android 10+ บาง API ถูกจำกัด ใช้
ConnectivityManagerตรวจสอบ Network แทนก็ได้
Manifest (Optional - สำหรับ Boot Receiver)
<receiver android:name=".AirplaneModeReceiver">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE" />
</intent-filter>
</receiver>
แนวทางปัจจุบัน: ใช้ ConnectivityManager
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); // หากไม่มี Network ใดๆ เชื่อมต่อ อาจเป็น Airplane Mode (แต่ไม่แน่ใจ 100%)