[英]How can I retrieve the title of the action button that has been clicked?
I am currently building an android app which needs to work through notification bar's action buttons. 我目前正在构建一个Android应用,该应用需要通过通知栏的操作按钮来工作。
I have added the notification bar and the action buttons. 我添加了通知栏和操作按钮。 I have also added a
BroadcastReceiver
to perform some action on button click. 我还添加了
BroadcastReceiver
来在单击按钮时执行一些操作。 My problem is that the actions are performed based on the title of the action button clicked. 我的问题是,操作是根据单击的操作按钮的标题执行的。 Please guide me in how to get the title of the clicked button.
请指导我如何获得单击按钮的标题。
This is my notification creation code: 这是我的通知创建代码:
Intent intent = new Intent();
intent.setAction(AppConstants.YES_ACTION);
// Open receiver
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//Create Notification using NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
// Set Icon
.setSmallIcon(R.mipmap.ic_launcher)
// Set Ticker Message
// .setTicker(getString(R.string.notificationticker))
// Set Title
.setContentTitle(getString(R.string.app_name))
// Set Text
.setContentText(getString(R.string.notificationtext))
// Add an Action Button below Notification
.addAction(R.mipmap.ic_launcher, "Action Button", pIntent)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Dismiss Notification
.setAutoCancel(true);
for(int l = 0; l<btnNames.length; l++){
if(!btnNames[l].isEmpty()){
builder.addAction(R.mipmap.ic_launcher, btnNames[l],pIntent);
// intent.putExtra("Btn"+(l+1), btnNames[1]);
}
}
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
Below is the broadcast receiver code: 下面是广播接收器代码:
public class notificationReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String notificationAction = intent.getAction();
//the title retrieval code goes here
if(notificationAction.equals(AppConstants.YES_ACTION)){
//some work is done here
}
}
}
Step #1: Move all of your Intent
/ PendingIntent
/ Notification
creation logic inside of the loop 步骤1:将所有
Intent
/ PendingIntent
/ Notification
创建逻辑移入循环
Step #2: Put an identifier of the action button in as an extra to the Intent
that you use for the PendingIntent
步骤#2:将操作按钮的标识符作为用于
PendingIntent
的Intent
的附加PendingIntent
Step #3: Use a distinct number for each PendingIntent
for the second parameter to the getBroadcast()
method, so you get different PendingIntent
instances for each 步骤#3:为
getBroadcast()
方法的第二个参数的每个PendingIntent
使用一个不同的数字,因此您将为每个参数获得不同的PendingIntent
实例
Step #4: Have your BroadcastReceiver
read the extra to determine the action that was clicked 步骤#4:让您的
BroadcastReceiver
阅读其他内容以确定被点击的操作
Or... 要么...
Step #1: Move all of your Intent
/ PendingIntent
/ Notification
creation logic inside of the loop 步骤1:将所有
Intent
/ PendingIntent
/ Notification
创建逻辑移入循环
Step #2: Use a unique action string in each of the Intent
objects 步骤2:在每个
Intent
对象中使用唯一的操作字符串
Step #3: Have your BroadcastReceiver
read the action string to determine the action that was clicked 步骤#3:让您的
BroadcastReceiver
读取操作字符串以确定被单击的操作
Or... 要么...
Step #1: Use different BroadcastReceivers
for each action button 步骤1:为每个操作按钮使用不同的
BroadcastReceivers
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.