Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close the status bar/notification panel after notification button click

I've browsed through Stackoverflow and have seen that this question has been asked, but I didn't find any solution.

I have a custom notification with 2 buttons. I want the status bar panel to close after I press a button on that notification, but don't know how. If I press the notification itself (so the contentIntent), then the panel closes, which is what I also want for my buttons.

Here's my notification code:

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);
like image 293
Toni Kanoni Avatar asked Mar 22 '13 11:03

Toni Kanoni


People also ask

How do I turn on notification status bar?

The user can reveal the Notifications window by pulling down the status bar (or selecting Notifications from the Home options menu).


3 Answers

Use the following two code line to collapse the notification bar.

Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(it);
like image 112
Sam Lu Avatar answered Oct 20 '22 14:10

Sam Lu


Ok I've found the solution. It's not public API, but it works (for the moment):

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

For this to work

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

is needed

like image 44
Toni Kanoni Avatar answered Oct 20 '22 16:10

Toni Kanoni


For Android 11 and below:

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

then:

Object statusBarService = context.getSystemService("statusbar");
Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
Method collapse = statusBarManager.getMethod("collapse");
collapse.invoke( statusBarService );

For Android 12 and above:

Either your app should be system app Then with the same permission used above the method will work

For third-party apps.. This working is no available

/**
 * Collapse the notifications and settings panels.
 *
 * Starting in Android {@link Build.VERSION_CODES.S}, apps targeting SDK level {@link
 * Build.VERSION_CODES.S} or higher will need {@link android.Manifest.permission.STATUS_BAR}
 * permission to call this API.
 *
 * @hide
 */
@RequiresPermission(android.Manifest.permission.STATUS_BAR)
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, publicAlternatives = "This operation"
        + " is not allowed anymore, please see {@link android.content"
        + ".Intent#ACTION_CLOSE_SYSTEM_DIALOGS} for more details.")
@TestApi
public void collapsePanels() {
  ...
}

and using Intent.ACTION_CLOSE_SYSTEM_DIALOGS

Refer this

like image 1
Jerin Avatar answered Oct 20 '22 14:10

Jerin