Android:以编程方式向通知添加按钮
问题描述:
我尝试以编程方式向通知添加Imagebuttons,但找不到此工作方法。我知道这是可能的,因为我看到similar appAndroid:以编程方式向通知添加按钮
这是我如何建立通知:
public class MyNotification extends Notification {
private Context ctx;
public Context getCtx() {
return ctx;
}
private NotificationManager mNotificationManager;
public MyNotification(Context ctx, int layout_id) {
super();
this.ctx = ctx;
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
CharSequence tickerText = "Shortcuts";
long when = System.currentTimeMillis();
Notification.Builder builder = new Notification.Builder(ctx);
Notification notification = builder.getNotification();
notification.when = when;
notification.tickerText = tickerText;
notification.icon = R.drawable.ic_launcher;
RemoteViews contentView = new RemoteViews(ctx.getPackageName(), layout_id);
//set button listners
setListeners(contentView);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(1387, notification);
}
,我怎么尝试添加的ImageButton
RemoteViews button = new RemoteViews(ctx.getPackageName(), R.layout.image_btn_layout_test);
Intent actionIntent = new Intent("MyIntent");
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, actionIntent, 0);
button.setOnClickPendingIntent(R.id.image, pendingIntent);
contentView.addView(R.layout.noti_layout, button);
答
通过这些问题来:
How to add button to notifications in android?
Adding button action in custom notification
Handling buttons inside android notifications
另请参阅Notification Actions开发人员指南。
此外它看起来像创建通知后不能添加操作,因此您应该创建具有指定操作的新通知,然后替换以前的通知(将id分配给您的通知)。
我在创建通知前添加按钮 – BrickT