31

我浏览了 Stackoverflow 并看到有人问过这个问题,但我没有找到任何解决方案。

我有一个带有 2 个按钮的自定义通知。我希望在按下该通知上的按钮后关闭状态栏面板,但不知道如何操作。如果我按下通知本身(即 contentIntent),则面板会关闭,这也是我想要的按钮。

这是我的通知代码:

Notification notification = new Notification(R.drawable.icon, "Service running", System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.contentIntent = openIntent;
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);


contentView.setOnClickPendingIntent(R.id.button1, stopIntent);
contentView.setOnClickPendingIntent(R.id.button2, addIntent);

notification.contentView = contentView;

notificationManager.notify(NOTIFICATION_ID, notification);
4

2 回答 2

120

使用以下两行代码折叠通知栏。

Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(it);
于 2013-12-06T09:36:33.917 回答
2

好的,我找到了解决方案。它不是公共 API,但它可以工作(目前):

Object sbservice = c.getSystemService( "statusbar" );
                    Class<?> statusbarManager = Class.forName( "android.app.StatusBarManager" );
                    Method hidesb = statusbarManager.getMethod( "collapse" );
                    hidesb.invoke( sbservice );

为了这个工作

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>

需要

于 2013-03-22T13:38:55.207 回答