简体   繁体   English

Android:如何使用取消按钮建立通知?

[英]Android: How to build a notification with a cancel button?

I am trying to create a notification with a cancel button, but when I click on it nothing happens. 我正在尝试创建一个带有取消按钮的通知,但是当我单击它时什么也没有发生。 This is my code: 这是我的代码:

private NotificationCompat.Builder notificationBuilder;

public void createNotification() {
    notificationBuilder = new NotificationCompat.Builder(service);
    notificationBuilder.setProgress(100, 0, false);
    notificationBuilder.setAutoCancel(true);
    Intent intent = OrderProcessingService_.intent(service).actionCancelUpload(basket.getPhotobook_id()).get();
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getService(service, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    notificationBuilder.setContentIntent(pendingIntent);
    notificationBuilder.setSmallIcon(R.drawable.ic_action_file_cloud_upload);

    NotificationCompat.Action cancelAction = new NotificationCompat.Action(R.drawable.ic_action_navigation_close, service.getString(R.string.cancel_upload), pendingIntent);
    notificationBuilder.setContentTitle(service.getString(R.string.notification_upload_title));
    notificationBuilder.setContentText(service.getString(R.string.notification_uploading_subtext, Util.getTitle(basket.getPhotobook())));
    notificationBuilder.addAction(cancelAction);
    addBigPicture(notificationBuilder);
 service.startForeground(1, notificationBuilder.build());
}

You will have to keep notification id and then call 您将必须保留通知ID,然后致电

NotificationManager nMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(notification );

I am glad to post it! 我很高兴发布它! After working all night I found something. 整晚工作后,我发现了一些东西。 So, here we go! 所以,我们开始!

  1. Create an xml layout file for your notification. 为您的通知创建一个xml布局文件。

  2. Create the notification using the Notification.Builder. 使用Notification.Builder创建通知。 After adding everything you want (icons, sounds, etc) do this: 添加所需的所有内容(图标,声音等)后,请执行以下操作:

     //R.layout.notification_layout is from step 1 RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.notification_layout); setListeners(contentView);//look at step 3 notification.contentView = contentView; 
  3. Create a method setListeners. 创建一个方法setListeners。 Inside this method you have to write this: 在此方法内部,您必须编写以下代码:

     //HelperActivity will be shown at step 4 Intent radio=new Intent(ctx, packagename.youractivity.class); radio.putExtra("AN_ACTION", "do");//if necessary PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0); //R.id.radio is a button from the layout which is created at step 2 view.setOnClickPendingIntent(R.id.radio, pRadio); //Follows exactly my code! Intent volume=new Intent(ctx, tsapalos11598712.bill3050.shortcuts.helper.HelperActivity.class); volume.putExtra("DO", "volume"); //HERE is the whole trick. Look at pVolume. I used 1 instead of 0. PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0); view.setOnClickPendingIntent(R.id.volume, pVolume); 
  4. For my requirements I used a HelperActivity which responds to the intents. 根据我的要求,我使用了一个HelperActivity来响应意图。 But for you I don't think it is necessary. 但是对于您来说,我认为没有必要。

If you want the full source code you can browse it or download it from my git repo. 如果您需要完整的源代码,则可以浏览或从我的git repo下载。 The code is for personal use, so don't expect to read a gorgeous code with a lot of comments. 该代码仅供个人使用,因此不要指望阅读带有很多注释的华丽代码。 https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts

ALL THE ABOVE, ANSWERS THE QUESTION OF CATCHING EVENT FROM DIFFERENT BUTTONS. 上面,回答了来自不同按钮的捕捉事件的问题。

About canceling the notification I redirect you here 关于取消通知,我在这里将您重定向

How to clear a notification in Android 如何清除Android中的通知

Just remember to use the id you parsed at the notify method when you called the notification for fist time 只需记住在第一次调用通知时使用在notify方法中解析的id

Source : Android notification with buttons on it 来源: 带有按钮的Android通知

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM