我试图在我的前台服务通知中添加一个停止按钮,所以我使用了广播接收器。但是当我在通知中单击“停止”按钮时,什么也没发生。
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!!!
,但它什么也没做。