บทนำ
ตัวอย่างนี้แสดงวิธีตรวจสอบสถานะการเชื่อมต่อ Wi-Fi แบบละเอียด (เช่น CONNECTED, CONNECTING, DISCONNECTED) โดยใช้ ConnectivityManager และ NetworkInfo.DetailedState
ขั้นตอนที่ 1: สร้างโปรเจกต์
Android Studio → New Project → Empty Activity
ขั้นตอนที่ 2: 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp" />
</LinearLayout>
ขั้นตอนที่ 3: โค้ดหลัก (MainActivity.java)
package com.example.myapplication;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
// ดึงสถานะโดยละเอียด เช่น CONNECTED, AUTHENTICATING, OBTAINING_IPADDR เป็นต้น
textView.setText("" + networkInfo.getDetailedState());
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
}
}
ขั้นตอนที่ 4: AndroidManifest.xml - Permission
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_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" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ผลลัพธ์


ค่าสถานะที่พบบ่อย (NetworkInfo.DetailedState)
| สถานะ | ความหมาย |
|---|---|
| CONNECTED | เชื่อมต่อสำเร็จ มี IP Address แล้ว |
| CONNECTING | กำลังเชื่อมต่อ |
| AUTHENTICATING | กำลังยืนยันตัวตน (รหัสผ่าน) |
| OBTAINING_IPADDR | กำลังขอ IP จาก DHCP |
| DISCONNECTED | ยกเลิกการเชื่อมต่อ |
| FAILED | เชื่อมต่อไม่สำเร็จ |
| SUSPENDED | ระงับชั่วคราว (เช่น Captive Portal) |
หมายเหตุสมัยใหม่
NetworkInfoถูก Deprecated ตั้งแต่ API 29 (Android 10)- ควรย้ายมาใช้
ConnectivityManager.NetworkCallbackและNetworkCapabilitiesแทน - ตัวอย่างนี้เหมาะสำหรับการเรียนรู้หรือรองรับ Android เก่า