android - 为什么我的 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>
谢谢你的帮助
解决方案
推荐阅读
- java - 使用 put() 更改 jsonObject 的值在 Java 中不起作用
- mysql - FETCH 未关联来自 mysql 表的 field_name
- javascript - 如何仅在 React 的特定组件中侦听事件?
- model-view-controller - 无法从下拉列表中选择值
- javascript - 如何正确导入“路由、路由器和交换机”
- adonis.js - 如何在 Adonis V5 中获取多对多数据?
- html - firebase 身份验证问题
- javascript - 混淆使用 JS vanilla 隐藏一些输入字段
- c++ - 为什么当 std::thread 用于多线程时,本征矩阵计算返回零?
- python-3.x - 不使用闭包的替代解决方案