点击Android通知操作不会closures通知抽屉

我正在使用NotificationCompat库向系统栏添加NotificationCompat 。 这个通知有两个操作button。 此外,通知上的AutoCancel()属性设置为true。

单击操作button时,系统configuration为启动一个调用NotificationManager.cancel(NOTIFICATION_ID)IntentService ,然后在新任务中启动一个活动。
问题是虽然这个调用从托盘中删除了通知,但它并没有折叠抽屉。 被调用的Activity被绘制在抽屉的后面。

有人可以请说明取消通知之后需要什么样的特殊代码才能结束抽签吗?

谢谢。

如果您的操作采用广播或服务的forms,但是您打算让通知抽屉崩溃,则应该从onReceive广播android.intent.action.CLOSE_SYSTEM_DIALOGS 。 这将手动closures抽屉。

我发现当你在扩展通知中使用动作button时,你必须编写额外的代码,而且你更受限制。

在使用扩展通知之前,我的文件下载通知中的默认操作是在文件上启动VIEW活动。 VIEW意图被Chooser意图包装。 我无法直接从通知中为Chooser使用意图,因为如果没有查看文件types的活动,选配器将崩溃。 所以我有一个BroadcastReceiver可以启动Chooser的意图。

通过扩展通知,我决定更改文件下载通知,因此默认操作是显示文件详细信息活动,并使用“查看和发送”的操作button。 如user2536953所示,从通知中启动广播接收器不会closures通知抽屉。 根据他的信息,一个活动将closures抽屉,我改变了我的广播接收器NotificationActivity没有任何用户界面。

正如在这篇文章中指出的那样,如果在点击操作之后closuresAndroid通知 ,另一个问题是当用户单击操作button时,您必须手动取消通知。 该通知仅在默认操作中被自动取消。 我还在NotificationActivity中添加了代码来处理这个问题。

使用查看和发送button构build扩展通知:

  NotificationCompat.Builder builder = new NotificationCompat.Builder(m_context).setAutoCancel(true); final PendingIntent contentIntent = DownloadedFileIntentUtils.buildPendingItemDetailIntent(m_context, item); builder.setContentIntent(contentIntent); PendingIntent viewIntent = DownloadedFileIntentUtils.buildNotificationActionIntent(m_context, Intent.ACTION_VIEW, m_context.getString(R.string.action_open), uri, MimeTypeUtil.getMimeType(item), id); builder.addAction(R.drawable.actionbar_open_with, m_context.getString(R.string.action_open), viewIntent); PendingIntent sendIntent = DownloadedFileIntentUtils.buildNotificationActionIntent(m_context, Intent.ACTION_SEND, m_context.getString(R.string.action_send), uri, MimeTypeUtil.getMimeType(item), id); builder.addAction(R.drawable.actionbar_share, m_context.getString(R.string.action_send), sendIntent); builder.setTicker(title) .setContentTitle(title) .setContentText(text) .setSmallIcon(R.drawable.notification_download); .setStyle(new NotificationCompat.BigTextStyle().bigText(text)); getNotificationManager().notify(id, builder.build()); 

通过通知操作button构build启动活动的意图:

  public static PendingIntent buildNotificationActionIntent(Context context, String action, String actionTitle, Uri uri, String mimeType, int notificationId) { // Build the file action intent (eg VIEW or SEND) that we eventually want to start. final Intent fileIntent = buildFileActionIntent(action, actionTitle, uri, mimeType); // Build the intent to start the NotificationActivity. final Intent notificationIntent = new Intent(context, NotificationActivity.class); // This flag must be set on activities started from a notification. notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Pass the file action and notification id to the NotificationActivity. notificationIntent.putExtra(Intent.EXTRA_INTENT, fileIntent); notificationIntent.putExtra(IIntentCode.INTENT_EXTRA_NOTIFICATION_ID, notificationId); // Return a pending intent to pass to the notification manager. return PendingIntent.getActivity(context, s_intentCode.getAndIncrement(), notificationIntent, PendingIntent.FLAG_ONE_SHOT); } public static Intent buildFileActionIntent(String action, String actionTitle, Uri uri, String mimeType) { Intent intent = new Intent(action); intent.addCategory(Intent.CATEGORY_DEFAULT); if (action.equals(Intent.ACTION_SEND)) { intent.putExtra(Intent.EXTRA_STREAM, uri); intent.setType(mimeType); } else { intent.setDataAndType(uri, mimeType); } intent.putExtra(Intent.EXTRA_TITLE, actionTitle); // Grant read permission on the file to other apps without declared permission. int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION; intent.setFlags(flags); return intent; } 

没有任何用户界面的通知活动:

 public class NotificationActivity extends Activity { private final static Logger s_logger = LogUtil.getLogger(NotificationActivity.class); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); // Cancel the notification that initiated this activity. // This is required when using the action buttons in expanded notifications. // While the default action automatically closes the notification, the // actions initiated by buttons do not. int notificationId = intent.getIntExtra(IIntentCode.INTENT_EXTRA_NOTIFICATION_ID, -1); if (notificationId != -1) { NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(notificationId); } // If there is an activity to handle the action, start the file action. if (DownloadedFileIntentUtils.verifyActivityIsAvailable(this, fileActionIntent, false)) { fileActionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); DownloadedFileIntentUtils.startFileActionActivity(this, fileActionIntent); } // Finish activity. finish(); } public static void startFileActionActivity(Context context, Intent fileActionIntent) { // Start chooser intent. Intent chooser = Intent.createChooser(fileActionIntent, fileActionIntent.getStringExtra(Intent.EXTRA_TITLE)); // Copy the flags from fileActionIntent to chooser intent. // FileActionExecutor must set FLAG_ACTIVITY_NEW_TASK on the intent passed to startActivity // because the flag is required when starting an activity from a context that is not an activity. chooser.addFlags(fileActionIntent.getFlags()); context.startActivity(chooser); } 

不要忘记将NotificationActivity添加到AndroidManifest.xml。

虽然我完全理解Soumyajyoti的答案应该是正确的,但实际上并不是这样。

我在用

 Intent CR = new Intent(CRCODE); PendingIntent.getBroadcast(getApplicationContext(), MY_ID, CR, 0); 

在正在进行通知的远程视图布局中

我可以向你保证,它不会closures抽屉,但会激发意图,并在打开的抽屉后面绘制活动,以及执行我分配给我的接收器的其他任务。

我已经testing了getActivity和getService(两者都不会在我的情况下工作,我需要激发多个意图onRecieve),他们都closures抽屉正确…

我知道这不是一个答案,但是,如果我发现它,我会报告回编辑这…

疯狂的想法…也许一个没有内容视图的活动可以被称为广播接收器,它会根据需要向其他类或应用程序发出适当的意图…

上面列出的疯狂的想法工作…开始与PendingIntent.getActivity活动,并使用它来中继广播或其他意图,然后完成自己…效果是不直接的,但不知道最终用户

当您点击通知并启动活动或广播时,android系统会为您通知通知抽屉。 我不认为有任何编程方式来closures您的代码通知抽屉。 我build议您检查您传递给通知构build器的意图,并确保给出正确的引用意图。 我认为,由于您提供的意图,android通知pipe理器中的内容会被阻止。 到目前为止,我还没有看到通知抽屉保持打开,一旦通知触发行动。 只是想知道你的目标活动是否有一个半透明的区域,其中前一个活动是可见的,如果是,那么我build议你使背景完全不透明(没有透明/半透明区域)。 可能是为了防止android主发射器完成停止序列,从而不让发射器为你closures通知抽屉。 希望我能帮助你。 祝一切顺利。