【问题标题】:Android: How to build a notification with a cancel button?Android:如何使用取消按钮构建通知?
【发布时间】:2015-09-22 08:18:11
【问题描述】:

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

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

【问题讨论】:

  • 更改此行 notificationBuilder.addAction(cancelAction);到 notificationBuilder.setDeleteIntent(cancelAction);告诉我你得到了什么
  • 如果更改notificationBuilder.addAction(cancelAction);到 notificationBuilder.setDeleteIntent(cancelAction);我没有取消按钮了。而且我没有向按钮添加 onClick() 事件,我认为标志 autoCancel = true 没有必要。
  • 对不起,我已经编辑了我的问题,因为我没有包含最后一行。我认为问题是通知是取消但在前台,意图仍在工作,我如何在取消按钮时调用 stopForeground被点击了??

标签: android android-notifications


【解决方案1】:

您必须保留通知 ID,然后调用

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

【讨论】:

  • 它没有用。似乎通知已取消,但在后台仍在运行,服务没有停止。
【解决方案2】:

我很高兴发布它!工作了一夜之后,我发现了一些东西。所以,我们开始吧!

  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 来响应意图。但对你来说,我认为没有必要。

如果你想要完整的源代码,你可以浏览它或者从我的 git repo 下载它。代码是供个人使用的,所以不要指望用大量的cmets来阅读华丽的代码。 https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts

以上所有内容,回答了从不同按钮捕获事件的问题。

关于取消通知我把你重定向到这里

如何在 Android 中清除通知

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

来源:Android notification with buttons on it

【讨论】:

  • 它没有用,最后我现在删除了按钮:(。就像我说的通知进度已停止但前台仍在工作..
  • 使用取消按钮,您需要使用挂起的意图并重定向到您需要取消通知 ID 的通知的某些活动。
猜你喜欢
  • 1970-01-01
  • 2014-10-02
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多