How to programmatically close the notification tray

I want to share a GCM notification item. The sharing button responds to click events, and the item is shared. Only problem here, the intent dialog is below the notification tray. The user must manually close the status bar, and then select the application to share. I want to close the status bar programmatically, so when a user clicks on sharing, he directly shows him a dialog for selecting applications.

I found that the status bar service can be used to open / close a service. But it is limited to system applications.

 private void closeNotificationTray() { Object service = mContext.getSystemService(Context.STATUS_BAR_SERVICE); Method collapse; try { Class<?> statusBarMngr = Class.forName("android.app.StatusBarManager"); if (Build.VERSION.SDK_INT >= 17) collapse = statusBarMngr.getMethod("collapsePanels"); else collapse = statusBarMngr.getMethod("collapse"); collapse.invoke(service); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } 

I used the code above. But I get the message "STATUS_BAR_SERVICE cannot be resolved." And when I added the lower resolution in the manifest:

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

I get it is allowed only for system applications. This does not allow me to use in my application. Is there a way to use the status bar service or any other alternative?

Update:

I solved the above problem with only two lines of code. There is no need to call STATUS_BAR_SERVICE.

 Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); mContext.sendBroadcast(it); 

Calling this intent automatically closes the notification

+6
source share
1 answer

Yes, this permission "android.permission.STATUS_BAR" is available only for system applications, and not for third-party applications.

You can try below:

  • Create some notice by sending pending intent to it.
  • Create a pending intent using stock as an action. Now you can use the share option.

@ Amanda Fernandez you can try the above method

0
source

Source: https://habr.com/ru/post/989655/


All Articles