Page 1 of 1

Es werden keine Ereignisse mit einem BroadcastReceiver empfangen

Posted: 05 Jan 2025, 17:15
by Guest
Ich habe zwei Klassen in meinem Android-Projekt:

Code: Select all

class NotificationEventListener: NotificationListenerService() {

override fun onNotificationPosted(sbn: StatusBarNotification?) {
super.onNotificationPosted(sbn)

Log.i("NotificationEventListener", "Sending broadcast")
sendBroadcast(
Intent("com.github.example.TEST")
)
}

override fun onNotificationRemoved(sbn: StatusBarNotification?) {
super.onNotificationRemoved(sbn)
}
}
Und:

Code: Select all

class WatchCompanionDeviceService : CompanionDeviceService() {
@SuppressLint("MissingPermission")
override fun onDeviceAppeared(associationInfo: AssociationInfo) {
super.onDeviceAppeared(associationInfo)
// Unrelated bluetooth code
}

override fun onDeviceDisappeared(associationInfo: AssociationInfo) {
super.onDeviceDisappeared(associationInfo)
// Unrelated bluetooth code
}

private val notificationReceiver = NotificationReceiver()

override fun onCreate() {
super.onCreate()
Log.i("WatchCompanionDeviceService", "CompanionDeviceService started")

// Notification Receiver
val notificationFilter = IntentFilter("com.github.example.TEST")
registerReceiver(notificationReceiver, notificationFilter, RECEIVER_NOT_EXPORTED)
Log.i("WatchCompanionDeviceService", "notificationFilter started")
}

override fun onDestroy() {
super.onDestroy()
Log.i("WatchCompanionDeviceService", "CompanionDeviceService stopped")

// Notification Receiver
unregisterReceiver(notificationReceiver)
}

class NotificationReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.i("WatchCompanionDeviceService", "Broadcast received")
}
}
}

Wenn ich die App starte und das Bluetooth-Gerät verbinde, werden CompanionDeviceService gestartet und notificationFilter gestartet Protokollmeldungen.

Wenn meine Das Telefon erhält eine Benachrichtigung. Ich erhalte die Protokollmeldung „Übertragung wird gesendet“.

Alles funktioniert also, außer dass die Übertragung nicht von WatchCompanionDeviceService empfangen wird, ich nie Holen Sie sich das Broadcast-Empfangen-Protokoll Nachricht.