[英]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.