处理Android自定义通知中的三个按钮单击事件 [英] Handle three buttons click event in custom notification in android
问题描述
我有一个带有三个按钮(确定",取消",稍后")的自定义通知 我只有在有一个按钮的情况下才能捕获单击事件,但是在这三个按钮中,我不知道单击了哪个按钮. 我的问题是如何知道单击了哪个按钮?"
I have a custom notification with three buttons ("ok", "cancel", "later") I can catch click event only when there is one button, but with the three buttons i can't know which button of them is clicked. My question is "How to know which button is clicked?"
// I use this code in My "createNotification" function to add event to buttons
Intent intent = new Intent(Context.NOTIFICATION_SERVICE);
PendingIntent pendinghomeIntent =
PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
notificationView.setOnClickPendingIntent(R.id.okButt, pendingrecentAppIntent);
notificationView.setOnClickPendingIntent(R.id.cancelButt, pendingrecentAppIntent);
notificationView.setOnClickPendingIntent(R.id.laterButt, pendingrecentAppIntent);
我创建了一个注册和注销广播接收器的服务,并且只需一个按钮即可完成. 使用这三个按钮时,当我单击它们的每一个时,都会发生相同的结果(在onReceive函数中实现), 但我希望他们每个人都有不同的结果.
I craeted a service to register and unregister the broadcast receiver and this is done perfectly in case of one button. With the three buttons, when i click on each of them the same result (which is implemeneted in onReceive function) is happened, but i want for each of them a different result.
请帮助我,谢谢!
推荐答案
您需要为每个按钮使用单独的PendingIntents
(由Intents
制成,并具有不同的动作).稍后在onReceive()
中,您只需检查传入的Intent
和& ;;的操作即可.依此执行不同的代码.
You need to use separate PendingIntents
(made from Intents
with differecnt actions) for every button. Later in onReceive()
you just check action of incoming Intent
& execute different code depending on that.
这篇关于处理Android自定义通知中的三个按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!