Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何对通知中通过xml添加的按钮执行自定义操作_Android_Notifications_Android Notifications - Fatal编程技术网

Android 如何对通知中通过xml添加的按钮执行自定义操作

Android 如何对通知中通过xml添加的按钮执行自定义操作,android,notifications,android-notifications,Android,Notifications,Android Notifications,我正在使用通知生成器通知设备上的通知。所有这些都很有效。我已经实现了悬而未决的意图。当任何用户单击通知时,Whcih会将用户带到活动。此外,当用户单击通知时,通过将auto cancelable属性设置为true,通知将根据我的需要消失 让我告诉你我开始通知的方式 mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mBuilder = new NotificationCo

我正在使用通知生成器通知设备上的通知。所有这些都很有效。我已经实现了悬而未决的意图。当任何用户单击通知时,Whcih会将用户带到活动。此外,当用户单击通知时,通过将auto cancelable属性设置为true,通知将根据我的需要消失

让我告诉你我开始通知的方式

 mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(DownloadService.this)
                            .setSmallIcon(R.drawable.download)
                            .setContentTitle("MyApps")
                            .setContentText("Downloading new tracks");
            mBuilder.setAutoCancel(true);


            Intent targetIntent = new Intent(DownloadService.this,TrackClass.class);
            PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder.setContentIntent(contentIntent);

            mNotifyManager.notify(1, mBuilder.build());
所以这一切都很好,我也在通知中显示进度和更新进度

我想要的是: 这就是为什么我贴出这个问题

我想在通知中添加按钮,以便 用户单击按钮,它将关闭服务。我有 对通知执行了单击操作,但我不知道 如何在其中添加按钮

请告诉我如何才能做到这一点

注意:请记住我正在制作和更新中的通知 服务

我想现在我很清楚我想要什么,我的问题是什么。 那么请告诉我如何在通知中添加按钮

编辑1:


我想知道,如果我实现了通知的自定义xml,那么其他属性,如在通知上显示我的应用程序名称和在通知上显示一条小的ticker消息,它们是否仍然能够显示在通知的旁边,或者我必须显式地显示它们?

您必须创建自己的自定义视图 所有需要的信息在这里http://developer.android.com/guide/topics/ui/notifiers/notifications.htmlUpdating


}

试试这个答案@mubee我以前见过这个链接好的,我会和你分享我的代码,但从编辑部分可以理解,如果你使用自定义布局,你就不能显示应用程序名称。你的意思是我们必须在xml中实现所有这些?按钮在哪里?
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
    .setContentTitle("New mail from " + "test@gmail.com")
    .setContentText("Subject").setSmallIcon(R.drawable.icon)
    .setContentIntent(pIntent)
    .addAction(R.drawable.icon, "Call", pIntent)
    .addAction(R.drawable.icon, "More", pIntent)
    .addAction(R.drawable.icon, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, noti);