如何使用onclick监听器在通知中添加自定义按钮?

时间:2017-08-14 09:17:14

标签: android

我有一个应用程序,我必须添加一个自定义布局与imageview,textview和按钮。为此,我使用了RemoteView。一切正常,但我想在按钮上添加点击监听器。我搜索了很多但没找到任何解决方案。我该怎么做?

private void customNotification(String title, int message, byte[] image) {
    // Using RemoteViews to bind custom layouts into Notification
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_card);
    builder = new NotificationCompat.Builder(this);

    builder.setSmallIcon(R.mipmap.ic_launcher_round);
    builder.setAutoCancel(false);
    builder.setOngoing(true);
    builder.setContent(remoteViews);
    builder.setCustomBigContentView(remoteViews);
    builder.setPriority(android.app.Notification.PRIORITY_MAX);

    Intent i = new Intent(context, CNotificationHomeActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(intent);

    // Locate and set the Image into customnotificationtext.xml ImageViews
    ByteArrayInputStream imageStream = new ByteArrayInputStream(image);
    Bitmap theImage = BitmapFactory.decodeStream(imageStream);
    remoteViews.setImageViewBitmap(R.id.imagenotileft, theImage);

    // Locate and set the Text into customnotificationtext.xml TextViews
    remoteViews.setTextViewText(R.id.title, title);
    if (message == 1) {
        remoteViews.setTextViewText(R.id.count_text, RewardUtil.fromHtml("<font color='#8AC34B'><b>" + message + "</b></font>" + " notification"));
    } else {
        remoteViews.setTextViewText(R.id.count_text, RewardUtil.fromHtml("<font color='#8AC34B'><b>" + message + "</b></font>" + " notifications"));
    }
    // Create Notification Manager
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Build Notification with Notification Manager
    notificationManager.notify(NOTIFICATION_ID, builder.build());

}

custom_notification_card.xml的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="8dp">

<ImageView
    android:id="@+id/imagenotileft"
    android:layout_width="50dp"
    android:layout_height="50dp" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/clean"
    android:layout_alignBottom="@+id/clean"
    android:layout_marginLeft="14dp"
    android:layout_marginStart="14dp"
    android:layout_toEndOf="@+id/imagenotileft"
    android:layout_toLeftOf="@+id/clean"
    android:layout_toRightOf="@+id/imagenotileft"
    android:layout_toStartOf="@+id/clean"
    android:ellipsize="end"
    android:lines="1"
    android:text="Title"
    android:textColor="@android:color/black" />

<Button
    android:id="@+id/clean"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="clean"
    android:textAllCaps="true"
    android:textSize="12dp" />


<TextView
    android:id="@+id/count_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/image_list"
    android:textColor="@android:color/black"
    android:textSize="14dp" />

0 个答案:

没有答案