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

时间:2015-09-22 08:18:12

标签: android android-notifications

我正在尝试使用取消按钮创建通知,但是当我点击它时没有任何反应。这是我的代码:

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());
}

2 个答案:

答案 0 :(得分:0)

您必须保留通知ID,然后致电

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

答案 1 :(得分:0)

我很高兴发布它!整晚工作后我发现了一些东西。所以,我们走了!

  1. 为通知创建xml布局文件。

  2. 使用Notification.Builder创建通知。添加所需的所有内容(图标,声音等)后,请执行以下操作:

    //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. 创建方法setListeners。在这个方法中你必须写这个:

    //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. 根据我的要求,我使用了一个响应意图的HelperActivity。但对你来说,我认为没有必要。

  5. 如果您需要完整的源代码,可以浏览它或从我的git repo下载它。该代码仅供个人使用,因此不要期望阅读带有大量注释的华丽代码。 https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts

    以上所有人都回答了不同按钮捕捉事件的问题。

    关于取消通知我在此重定向

    如何清除Android中的通知

    请记住在第一次调用通知时使用您在notify方法中解析的ID

    来源:Android notification with buttons on it