如何使用附加按钮从自定义丰富通知中关闭 Android >= 3.0 中的通知下拉菜单?
我成功地为 Android >= 3.0 创建了一个自定义丰富通知,其中显示了一些文本和一个附加按钮。如果您单击通知下拉列表中按钮以外的任何位置的通知,则通知将被忽略,下拉列表将关闭,并且指定的 Intent 将按预期启动。 如果单击通知中的专用按钮,则会成功启动不同的 Intent,但下拉列表保持打开状态(通知仍然存在,但我稍后取消它,这不是问题)。意图启动一个出现在通知下拉列表后面的活动。
我想要实现的目标是保留所描述的所有当前行为,同时关闭按钮启动的意图中的通知下拉菜单 - 这可能吗?或者,如果按钮 Intent 中的 Activity 获得窗口焦点就足够了。
这里是自定义通知的代码,如果有帮助的话:
Notification.Builder builder = new Notification.Builder(main)
.setSmallIcon(R.drawable.notification)
.setAutoCancel(true)
.setTicker(text)
.setContentIntent(...);
RemoteViews layout = new RemoteViews(
main.getPackageName(), R.layout.notification);
layout.setTextViewText(R.id.title, title);
layout.setTextViewText(R.id.text, text);
Intent i = new Intent(
"snooze", null, main, Snooze.class
);
i.putExtra(KEY_WIDGET_ID, widgetId);
layout.setOnClickPendingIntent(R.id.notification_zzz, PendingIntent.getActivity( main, 0, i, PendingIntent.FLAG_UPDATE_CURRENT ));
builder.setContent(layout);
...
NotificationManager nm =
(NotificationManager)main.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, builder.getNotification());
I successfully created a custom rich notification for Android >= 3.0 that shows some text and an additional button. If you click the notification anywhere but on the button in the notification drop-down, the notification is dismissed, the drop-down closed and the specified Intent is launched as expected.
If the dedicated button in the notification is clicked, a different Intent is successfully launched, but the drop-down keeps open (and the notification is still there, but I cancel it later, that is not the problem). The Intent launches an Activity which appears behind the notifications drop-down.
What I like to achieve is to keep all the current behavior as described, but also close the notification drop-down from the Intent the button launches - is this possible? Alternatively it would be enough if the Activity from the button Intent gains the window focus.
Here the code for the custom notification, if that helps:
Notification.Builder builder = new Notification.Builder(main)
.setSmallIcon(R.drawable.notification)
.setAutoCancel(true)
.setTicker(text)
.setContentIntent(...);
RemoteViews layout = new RemoteViews(
main.getPackageName(), R.layout.notification);
layout.setTextViewText(R.id.title, title);
layout.setTextViewText(R.id.text, text);
Intent i = new Intent(
"snooze", null, main, Snooze.class
);
i.putExtra(KEY_WIDGET_ID, widgetId);
layout.setOnClickPendingIntent(R.id.notification_zzz, PendingIntent.getActivity( main, 0, i, PendingIntent.FLAG_UPDATE_CURRENT ));
builder.setContent(layout);
...
NotificationManager nm =
(NotificationManager)main.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, builder.getNotification());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Android开发者办公时间提问:
http://www.youtube.com/watch?v=XvLBvdml_Fs(问题从 49 开始) :10)
答案是,这是不可能的,我什至不应该这样做,我对此表示怀疑。
这就是为什么我创建了一个功能请求:
http://code.google.com/p/android/issues/detail ?id=24762
编辑 2012 年 8 月 10 日:
从 Android 4.1 开始,这是可能的,因为通知现在可以包含其他操作。请参阅以下页面了解更多信息:
https://developer.android.com/about/versions/android-4.1 .html#UI
Asked the question in the Android developer office hours:
http://www.youtube.com/watch?v=XvLBvdml_Fs (question starting 49:10)
The answer was, that it is not possible and I should not even do this, which I question.
That is why I have created a feature request:
http://code.google.com/p/android/issues/detail?id=24762
EDIT 08-10-12:
Starting with Android 4.1 this is possible as notifications can now include additional actions. See the following page for more information:
https://developer.android.com/about/versions/android-4.1.html#UI