ব্রডকাস্ট রিসিভার দিয়ে নোটিফিকেশন টগল (শুরু/বন্ধ)
এই উদাহরণে একটি USB স্টেট ব্রডকাস্ট রিসিভার ব্যবহার করে নোটিফিকেশন দেখানো এবং লুকানো দেখানো হয়েছে। USB ক্যাবল লাগালে নোটিফিকেশন আসবে, খোললে চলে যাবে।
আর্কিটেকচার
- USB_STATE ব্রডকাস্ট — সিস্টেম পাঠায় USB সংযোগ/বিচ্ছিন্ন হলে
- USBStateReceiver — ইন্টেন্ট পায়, স্টেট অনুযায়ী নোটিফিকেশন টগল করে
- Boolean ফ্ল্যাগ — connected/disconnected স্টেট ট্র্যাক করে
ধাপ ১: প্রজেক্ট তৈরি
Package: com.example.usbnotificationtoggle
ধাপ ২: লেআউট (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"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="USB ক্যাবল লাগালে নোটিফিকেশন আসবে, খোললে যাবে"
android:textSize="16sp"
android:gravity="center"
android:padding="16dp"/>
</RelativeLayout>
ধাপ ৩: MainActivity.java (খালি)
package com.example.usbnotificationtoggle;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ধাপ ৪: USBStateReceiver.java — টগল লজিক
package com.example.usbnotificationtoggle;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;
import androidx.core.app.NotificationCompat;
public class USBStateReceiver extends BroadcastReceiver {
public static final String CHANNEL_ID = "usb_notif_channel";
private static final String DEFAULT_CHANNEL = "default";
private static final String PREFS_NAME = "UsbNotifState";
private static final String KEY_CONNECTED = "connected";
private static final int NOTIFICATION_ID = 1;
@SuppressLint("UnsafeProtectedBroadcastReceiver")
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("USBToggle", "Action: " + action);
if (action == null) return;
NotificationManager manager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
ensureChannel(manager);
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
boolean connected = prefs.getBoolean(KEY_CONNECTED, true);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, DEFAULT_CHANNEL)
.setContentTitle("USB - নোটিফিকেশন")
.setContentText("সংযুক্ত")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setAutoCancel(true)
.setChannelId(CHANNEL_ID);
Notification notification = builder.build();
if (Intent.ACTION_USB_STATE.equals(action)) {
boolean usbConnected = intent.getBooleanExtra("connected", false);
if (usbConnected && connected) {
// USB লাগালো, আগে disconnected ছিল → দেখান
manager.notify(NOTIFICATION_ID, notification);
prefs.edit().putBoolean(KEY_CONNECTED, false).apply();
Log.d("USBToggle", "Notification SHOWN");
} else if (!usbConnected && !connected) {
// USB খোলো, আগে connected ছিল → মুছুন
manager.cancel(NOTIFICATION_ID);
prefs.edit().putBoolean(KEY_CONNECTED, true).apply();
Log.d("USBToggle", "Notification CANCELLED");
}
}
}
private void ensureChannel(NotificationManager manager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID, "USB নোটিফিকেশন",
NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
}
}
ধাপ ৫: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.usbnotificationtoggle">
<uses-permission android:name="android.permission.VIBRATE" />
<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/Theme.UsbNotificationToggle">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".USBStateReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
কীভাবে কাজ করে
- USB ক্যাবল লাগালে → সিস্টেম `android.hardware.usb.action.USB_STATE` ব্রডকাস্ট পাঠায়
- Receiver `onReceive()`-এ পায় → `intent.getBooleanExtra("connected", false)` চেক করে
- Connected=true এবং আগে disconnected ছিল → `manager.notify()`
- Connected=false এবং আগে connected ছিল → `manager.cancel()`
- SharedPreferences-এ স্টেট সেভ থাকে প্রসেস রিস্টার্ট জুড়ে
সীমাবদ্ধতা ও উন্নতি
| সীমাবদ্ধতা | উন্নতি |
|---|---|
| শুধু USB_STATE কাজ করে | BOOT_COMPLETED যোগ করে রিবুটে রিস্টোর করুন |
| SharedPreferences প্রসেস রিস্টার্টে রিসেট হতে পারে | DataStore বা Room DB ব্যবহার করুন |
| নোটিফিকেশন সোয়াইপ করলে গেলে স্টেট সিঙ্ক বাইরে | setDeleteIntent দিয়ে স্টেট আপডেট করুন |