How to hide Notification Panel after sending a Pen

2019-02-20 03:37发布

问题:

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 use remoteView to have three images with three OnClickPendingIntent. One of them is sending back to the service, and do the notification update codes.

code for creating notification:

 Intent intentApp = new Intent(this,ScreenOffWidgetConfigure.class);
 intentApp.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
 PendingIntent piApp = PendingIntent.getActivity(this, 0, intentApp, 0);

 Intent intentOnOff = new Intent(CV.SERVICE_INTENT_ACTION);
 intentOnOff.putExtra(CV.SERVICEACTION, CV.SERVICEACTION_TOGGLE);
 PendingIntent piOnOff = PendingIntent.getService(this, 0, intentOnOff, 0);

 Intent intentScreenOff = new Intent(CV.SERVICE_INTENT_ACTION);
 intentScreenOff.putExtra(CV.SERVICEACTION, CV.SERVICEACTION_SCREENOFF);
 PendingIntent piScreenOff = PendingIntent.getService(this, 1, intentScreenOff, 0);

 // setup remoteview
 RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);
 remoteViews.setOnClickPendingIntent(R.id.image_logo, piApp);
 remoteViews.setOnClickPendingIntent(R.id.image_status, piOnOff);
 remoteViews.setOnClickPendingIntent(R.id.image_screenoff, piScreenOff);

 if(CV.getPrefChargingOn(this) && CV.isPlugged(this))
 {
     remoteViews.setImageViewResource(R.id.image_status,R.drawable.widget_charging_on);
 }
 else
 {
     if(CV.getPrefAutoOnoff(this))
     remoteViews.setImageViewResource(R.id.image_status,R.drawable.widget_on);
     else
     remoteViews.setImageViewResource(R.id.image_status,R.drawable.widget_off);
 }

 // build the notification
 Notification noti = new Notification.Builder(this)
 .setContent(remoteViews)
 .setSmallIcon(R.drawable.ic_launcher)
 .setOngoing(true)
 .build();

 return noti;

code for updating notificaton, after receiving the intent:

 Notification notify = createNotification();
 final NotificationManager notificationManager = (NotificationManager) getApplicationContext()
                        .getSystemService(getApplicationContext().NOTIFICATION_SERVICE);

 notificationManager.notify(NOTIFICATION_ONGOING, notify);

My question is: after calling my updating function, the notification image is actually changed; however, the notification panel is still there!! It does not disappear just like what it should be for launching activities.

Is there a flag I can set for notification, pendingIntent, or what API calls I can use after receiving the intent in service?

回答1:

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.