How to hide Notification Panel after sending a PendingIntent to Service in a notification

后端 未结 1 1440
眼角桃花
眼角桃花 2021-01-15 02:19

All

I write a service to to update system state, and I use startForeground to put the service to foreground, also adding a notification to it. In the notification, I

相关标签:
1条回答
  • 2021-01-15 02:47

    find my question answered in other place:
    1. Android: How to collapse status bar on android 4.2? 2. Preventing status bar expansion

    first, add use permission lines to AndroidManifest.xml

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

    second, use following codes to make it work:

    try
    {
        Object service  = getSystemService("statusbar");
        Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
        if (Build.VERSION.SDK_INT <= 16) 
        {
            Method collapse = statusbarManager.getMethod("collapse");
            collapse.setAccessible(true);
            collapse.invoke(service);
        } 
        else 
        {
            Method collapse2 = statusbarManager.getMethod("collapsePanels");
            collapse2.setAccessible(true);
            collapse2.invoke(service);
        }
    }catch(Exception ex){}
    

    it's hacky, but it did work.

    0 讨论(0)
提交回复
热议问题