android - 未调用 PendingIntent.getBoradcast 广播接收器
问题描述
我正在尝试在前台服务通知中添加一个停止按钮,所以我使用了广播接收器。但是当我点击通知中的停止按钮时,什么都没有发生。
val stopIntent = Intent(this, StopBroadCastReceiver::class.java)
stopIntent.action = STOP_SERVICE
val stopPendingIntent =
PendingIntent.getBroadcast(applicationContext, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT)
notification
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("Pyrum is running...")
.setSubText("192.168.1.67")
.setOnlyAlertOnce(true)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.stop), stopPendingIntent)
.priority = NotificationCompat.PRIORITY_MIN
startForeground(NOTIFICATION_ID, notification.build())
广播接收器
inner class StopBroadCastReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("hello", "STOP!!!")
}
}
它应该记录STOP!!!
,但它什么也没做。
解决方案
推荐阅读
- c# - 通过 nvidia quadro 600 gpu 模式访问时 C# DOT NET 应用程序崩溃 - nvd3dum.dll 崩溃
- php - 如何从PHP中的文本中提取名称
- c++ - 如何让我的编译器更愚蠢(错误的索引)?
- html - sed 命令处理输入流并用标签替换所有HTML标签
- angular - selectionChange 不会为 mat-select 中的默认选项发出 - Angular Material
- aframe - 依赖于具有多个 true 的组件的 Aframe 组件
- python - 为什么 Python 生成的 ELF 比原始源代码大?
- swift - 如何在 macOS 上的照片应用中访问照片
- javascript - 为什么绝对定位指示符会超出相对定位容器?
- mongodb - 聚合上的未闭合括号