简体   繁体   English

如何通过单击通知中的操作按钮执行自定义任务?

[英]How to perform a custom task from a action button click in notification?

I have a notification in my app and I want to perform a custom task on clicking of the action in the notification.我的应用程序中有一个通知,我想在单击通知中的操作时执行自定义任务。 I have done this to add a action:我这样做是为了添加一个动作:

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

But, I want to stop a handler from running on click of this action.但是,我想阻止处理程序在单击此操作时运行。 I have only seen to open activities using this.我只看到使用它打开活动。 But, how to stop a handler or any custom task?但是,如何停止处理程序或任何自定义任务?

Thanks谢谢

@Sambhav.K, You need to pass Pending Intent in action button like below code @Sambhav.K,您需要像下面的代码一样在操作按钮中传递 Pending Intent

  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
    )

and create new CustomActionReceiver class and do your stuff what you want like below并创建新的 CustomActionReceiver class 并像下面那样做你想做的事情

class CustomActionReceiver : BroadcastReceiver() {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM