【发布时间】:2018-01-29 11:34:46
【问题描述】:
我目前正在构建一个需要通过通知栏的操作按钮工作的 Android 应用。
我添加了通知栏和操作按钮。我还添加了一个BroadcastReceiver 来对按钮单击执行一些操作。我的问题是操作是根据单击的操作按钮的标题执行的。请指导我如何获取点击按钮的标题。
这是我的通知创建代码:
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());
下面是广播接收器代码:
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
}
}
}
【问题讨论】:
-
btnNames 是一个字符串数组。我使用这些作为我的操作按钮名称。它是动态加载的。
标签: android notifications android-notifications