如何通过操作按钮点击通知执行自定义任务



我的应用程序中有一个通知,我想在单击通知中的操作时执行自定义任务。我这样做是为了添加一个动作:

timerNotificationBuilder.addAction(0, "STOP", null /*What to add here ?*/ );

但是,我想在单击此操作时阻止处理程序运行。我只见过用这个打开活动。但是,如何停止处理程序或任何自定义任务?

感谢

@Sambhav.K,您需要传递Pending Intent in action按钮,如下面的代码

notificationBuilder.addAction(mCallAction)
val mCallAction = NotificationCompat.Action.Builder(
R.drawable.ic_reject_call,
"Stop",
mPendingIntent
)

val mCallIntent = Intent(context,   CustomActionReceiver::class.java)

val mPendingIntent = PendingIntent.getBroadcast(
context, 999,
mCallIntent, PendingIntent.FLAG_UPDATE_CURRENT
)

创建新的CustomActionReceiver类,并在下随心所欲

class CustomActionReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// Do your Stuff
}}

最新更新