পূর্ব প্রস্তুতি
এই উদাহরণটি `BroadcastReceiver` ব্যবহার করে হেডসেট (হেডফোন) প্লাগ-ইন/আউট ইভেন্ট সনাক্ত করার পদ্ধতি দেখায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project নির্বাচন করুন।
ধাপ ২: 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:id="@+id/text"
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Headphones connection status"
android:textColor="@android:color/holo_purple"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
ধাপ ৩: MainActivity.kt-এ BroadcastReceiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private var broadcastReceiver: BroadcastReceiver? = null
private var microphonePluggedIn = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
val action = intent.action
if (Intent.ACTION_HEADSET_PLUG == action) {
val state = intent.getIntExtra("state", -1)
when (state) {
0 -> {
microphonePluggedIn = false
Toast.makeText(applicationContext, "Headphones not plugged in", Toast.LENGTH_LONG).show()
}
1 -> {
microphonePluggedIn = true
Toast.makeText(applicationContext, "Headphones plugged in", Toast.LENGTH_LONG).show()
}
}
}
}
}
val receiverFilter = IntentFilter(Intent.ACTION_HEADSET_PLUG)
registerReceiver(broadcastReceiver, receiverFilter)
}
override fun onDestroy() {
super.onDestroy()
broadcastReceiver?.let { unregisterReceiver(it) }
}
}
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.headsetdetection">
<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>
</application>
</manifest>
পরীক্ষা করুন
অ্যাপ চালান। হেডফোন প্লাগ-ইন/আউট করলে টোস্ট মেসেজে স্ট্যাটাস দেখাবে। ম্যানিফেস্টে `
সতর্কতা
Android 10+ (API 29+) এ `ACTION_HEADSET_PLUG` ব্রডকাস্ট পাওয়া যেতে পারে না প্রাইভেসি পাবন্দির কারণে। অডিও ডিভাইস ম্যানেজার (`AudioManager`) ব্যবহার করা ভালো।