繁体   English   中英

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

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

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

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

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

谢谢

@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
    )

并创建新的 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