如何在Android通知中添加按钮

如何在Android通知中添加按钮,android,android-layout,notifications,Android,Android Layout,Notifications,我正在尝试在每个通知中添加一个按钮。。。用户可以点击按钮删除单个通知,我看到很多人说只需参考“创建自定义扩展视图”和使用RemoteView,但是否可以修改官方代码并让按钮工作? 我使用imagebutton在“status\u bar\u latest\u event\u context.xml”中添加了这个按钮 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_co

我正在尝试在每个通知中添加一个按钮。。。用户可以点击按钮删除单个通知,我看到很多人说只需参考“创建自定义扩展视图”和使用RemoteView,但是否可以修改官方代码并让按钮工作? 我使用imagebutton在“status\u bar\u latest\u event\u context.xml”中添加了这个按钮

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="3dp"
    >
    <!--com.android.server.status.AnimatedImageView android:id="@+id/icon" -->
    <ImageView android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:scaleType="fitCenter"
        android:src="@drawable/arrow_down_float"/>
    <TextView android:id="@+id/title"
        android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:paddingLeft="4dp"
        />
     <ImageButton android:id="@+id/imgbtn_del" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/btn_close"/>

</LinearLayout>

它将单击事件绑定到内容区域。所以我不能点击按钮。 我不知道如何修改源代码以及如何设置OnClick函数

请帮忙。。。
谢谢太多了

我希望这能对你有所帮助

//Exemple of notification with Button
private static void scheduleNotificationWithButton(Context context, String message) {

    int notifReCode = 1;

    //What happen when you will click on button
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT);

    //Button
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.my_image, "Delete", pendingIntent).build();

    //Notification
    Notification notification = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentText("Back to Application ?")
            .setContentTitle("Amazing news")
            .addAction(action) //add buton
            .build();

    //Send notification
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}

我希望这能帮助你

//Exemple of notification with Button
private static void scheduleNotificationWithButton(Context context, String message) {

    int notifReCode = 1;

    //What happen when you will click on button
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT);

    //Button
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.my_image, "Delete", pendingIntent).build();

    //Notification
    Notification notification = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentText("Back to Application ?")
            .setContentTitle("Amazing news")
            .addAction(action) //add buton
            .build();

    //Send notification
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}