【问题标题】:Android: programmatically adding buttons to notificationAndroid:以编程方式向通知添加按钮
【发布时间】:2016-07-22 20:13:37
【问题描述】:

我正在尝试以编程方式将 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);

【问题讨论】:

    标签: android notifications remoteview


    【解决方案1】:

    通读这些问题:

    How to add button to notifications in android?
    Adding button action in custom notification
    Handling buttons inside android notifications

    还可以查看Notification Actions 开发人员指南。

    此外,在创建通知后您似乎无法添加操作,因此您应该使用指定的操作创建新通知,然后替换之前的通知(为您的通知分配 id)。

    【讨论】:

    • 我在创建通知之前添加按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    相关资源
    最近更新 更多