ภาพรวม
ตัวอย่างนี้สาธิตวิธีการตรวจจับสายโทรศัพท์เข้า (Incoming Call) ใน Android โดยใช้ BroadcastReceiver ฟัง Intent android.intent.action.PHONE_STATE
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
สร้างโปรเจกต์ Android ใหม่ใน Android Studio
ขั้นตอนที่ 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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="แอปตรวจจับสายเข้า - โทรทดสอบได้เลย"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:layout_centerInParent="true" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity.java (ขอสิทธิ์)
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_READ_PHONE_STATE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkAndRequestPermission();
}
private void checkAndRequestPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE},
PERMISSION_REQUEST_READ_PHONE_STATE);
} else {
Toast.makeText(this, "พร้อมตรวจจับสายเข้าแล้ว", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_READ_PHONE_STATE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "อนุญาตสิทธิ์แล้ว - พร้อมตรวจจับสายเข้า", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "ต้องอนุญาตสิทธิ์ READ_PHONE_STATE", Toast.LENGTH_SHORT).show();
}
}
}
}
ขั้นตอนที่ 4: BroadcastReceiver (src/DetectCalls.java)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class DetectCalls extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
// สายโทรเข้า - กำลังริง
String msg = "📞 สายเข้าจาก: " + (incomingNumber != null ? incomingNumber : "ไม่ทราบหมายเลข");
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
// สามารถดำเนินการอื่นๆ ได้ที่นี่ เช่น:
// - บันทึก Log
// - ส่ง Notification
// - บล็อกสาย (ต้องการสิทธิ์เพิ่มเติม)
} else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
// สายกำลังคุยอยู่
Toast.makeText(context, "📞 กำลังคุยอยู่...", Toast.LENGTH_SHORT).show();
} else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
// สายจบ/วางเครื่อง
Toast.makeText(context, "📴 สายจบการโทร", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
ขั้นตอนที่ 5: 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.READ_PHONE_STATE"/>
<!-- สำหรับ Android 10+ ต้องการสิทธิ์เพิ่ม -->
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<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>
<!-- 🔑 ลงทะเบียน BroadcastReceiver -->
<receiver android:name=".DetectCalls" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
⚠️ ข้อจำกัดและการเปลี่ยนแปลงใน Android ใหม่
Android 10 (API 29) +: ต้องการสิทธิ์
Android 11 (API 30) +: BroadcastReceiver สำหรับ PHONE_STATE ต้อง宣告ใน Manifest ด้วย
Android 12 (API 31) +: จำกัดการเข้าถึงหมายเลขโทรศัพท์ (EXTRA_INCOMING_NUMBER อาจคืนค่า null)
READ_CALL_LOG และ READ_PHONE_STATE พร้อมกันAndroid 11 (API 30) +: BroadcastReceiver สำหรับ PHONE_STATE ต้อง宣告ใน Manifest ด้วย
android:exported="true"Android 12 (API 31) +: จำกัดการเข้าถึงหมายเลขโทรศัพท์ (EXTRA_INCOMING_NUMBER อาจคืนค่า null)
วิธีใหม่: ใช้ CallScreeningService (API 26+)
สำหรับแอป Call Blocker/Screener ควรใช้ CallScreeningService ซึ่งทรงพลังกว่าและปลอดภัยกว่า
การทดสอบ
รันแอป ให้สิทธิ์ โทรหาเครื่องทดสอบ จะแสดง Toast แจ้งหมายเลขโทรเข้า
