Android: 在程序中向通知栏动态添加按钮

3
我正在尝试以编程方式向通知中添加Imagebuttons,但是我找不到适用的工作方法。我知道这是可能的,因为我看到了类似应用程序similar app
这是我构建通知的方式:
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);
1个回答

3

1
在创建通知之前,我会添加按钮。 - BrickT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接