如何在通知按钮单击后关闭状态栏/通知面板

Ton*_*oni 25 notifications android statusbar

我浏览过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);
Run Code Online (Sandbox Code Playgroud)

Sam*_* Lu 104

使用以下两个代码行来折叠通知栏.

Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(it);
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的答案,因为此代码无需私有API即可运行. (2认同)

Ton*_*oni 3

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

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

为了这个工作

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
Run Code Online (Sandbox Code Playgroud)

需要