首页 > 解决方案 > 为什么我的 Android BroadcastReceiver 消息没有到达?

问题描述

BroadcastReceiver这是关于为什么看不到发送给它的消息的另一个问题。我查看了十几个示例和其他问题,他们的解决方案似乎与我的相同,但我的不起作用。

我的接收器正在服务中。这是清单片段:

<service android:name="com.madurasoftware.vmnotifications.services.BLEService"
    android:label="BluetoothService"
    android:process="com.madurasoftware.vmnotifications.services.VMServices">
</service>

服务像这样注册接收者:

    private val messageReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            Log.d(TAG, "onReceive() called")
            val message = intent.getStringExtra(BluetoothService.MESSAGE)
            NotificationQueue.add(message)
        }
    }
    override fun onCreate() {
        super.onCreate()
        Log.d(TAG, "onCreate() called")
        val filter = IntentFilter()
        filter.addAction(BluetoothService.MESSAGE_ACTION)
        baseContext.registerReceiver(this.messageReceiver, filter )
    }

    override fun onDestroy() {
        baseContext.unregisterReceiver(this.messageReceiver)
    }

因为我正在动态注册接收器,所以我没有将任何关于它的内容添加到清单中。我MainActivity像这样从一个按钮发送:

            Intent().also { intent ->
                intent.setAction(BluetoothService.MESSAGE_ACTION)
                intent.putExtra(BluetoothService.MESSAGE, "xyz")
                sendBroadcast(intent)
            }
            Log.d(TAG, "sendBroadcast() called")

当我按下按钮时,我看到记录的sendBroadcast()消息出现了,但onReceive()日志消息没有出现。所以没有发货。谁能明白为什么?我正在使用 API 29,这是我的完整清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.madurasoftware.vmnotifications">
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-feature
            android:name="android.hardware.bluetooth_le"
            android:required="true" />

    <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"
            android:fullBackupContent="@xml/my_backup_rules">

        <activity android:name=".ui.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <service android:name="com.madurasoftware.vmnotifications.services.NotificationService"
                 android:label="@string/app_name"
                 android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>

        <service android:name="com.madurasoftware.vmnotifications.services.BLEService"
                 android:label="BluetoothService"
                 android:process="com.madurasoftware.vmnotifications.services.VMServices">
        </service>

        <service android:name="com.madurasoftware.vmnotifications.services.BluetoothService"
                 android:label="BluetoothService"
                 android:process="com.madurasoftware.vmnotifications.services.VMServices">
        </service>

    </application>

</manifest>

谢谢你的帮助

标签: androidkotlinandroid-service

解决方案


推荐阅读