Android 仅在状态栏展开时显示通知

Android show notification only when the status bar is expanded

是否可以在 Android 中仅在状态栏展开时显示通知?我的意思是只有当我向下拖动状态栏时,我才能看到通知,否则它会被隐藏。如果可能的话,我该如何实施?我只需要在最小视图中隐藏状态栏中的应用程序图标即可。Check this

您可以将 builder.setSmallIcon(...) 添加到您正在使用的 Notification.BuilderNotificationCompat.Builder 中吗?

尝试在通知中获取小图标所需的代码:

   // Use NotificationCompat.Builder to set up our notification.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
   /* icon appears in device notification bar and right hand corner of
     notification */
    builder.setSmallIcon(R.drawable.ic_launcher);//Set this
   .....

实际上我自己发现,从 Android 4.1 (API 16) 开始,可以指定通知的优先级。如果将该标志设置为 PRIORITY_MIN,通知图标将不会显示在状态栏上,但您仍然可以在展开状态栏时查看通知。

// Use NotificationCompat.Builder to set up our notification.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            this).setOngoing(true);
    notificationBuilder.setContentTitle("my_title"); 
    notificationBuilder.setSmallIcon(ic_launcher);
// Do other stuffs here.

 notificationBuilder.setPriority(Notification.PRIORITY_MIN);// Set this.