在状态栏中仅显示一个小图标,而不显示通知 [英] Show just small icon in status bar without showing notification

查看:510
本文介绍了在状态栏中仅显示一个小图标,而不显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在status bar中始终显示小图标,而不在系统的notification托盘上显示通知本身.

I want to show constantly small icon in the status bar, without show the notification itself on the notification tray of the system.

我尝试使用自定义layout并将根元素的可见性设置为View.GONEView.INVISIBLE,但是对于这两者,系统都将其忽略.

I tried using custom layout and set the visibility of the root element to View.GONE, or View.INVISIBLE, but for both, the system ignores it.

我也尝试设置根元素的高度,但是系统也忽略了这一点.

I also tried to set the height of the root element but the system ignores this too.

这是我的代码:

R.layout.custom_notification:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/custom_notification"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/notif_background"
    android:orientation="horizontal"
    android:baselineAligned="false"/>

以及用于显示Notification的代码:

 NotificationManager nm = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);
 mBuilder
         .setSmallIcon(drawableId)
         .setAutoCancel(false)
         .setOngoing(true)
         .setPriority(Notification.PRIORITY_MAX);
 Intent resultIntent = new Intent(ctx, SettingsActivity.class);
 PendingIntent resultPendingIntent = PendingIntent.getActivity(ctx, 0, resultIntent, 0);
 mBuilder.setContentIntent(resultPendingIntent);
 int layout =  R.layout.custom_notification ;
 RemoteViews contentView = new RemoteViews(ctx.getPackageName(), layout);
 contentView.setViewVisibility(R.id.custom_notification, View.INVISIBLE);
 mBuilder.setContent(contentView);
 nm.notify(NOTIFICATION_ID, mBuilder.build());

我很清楚,为了实现我的目标,我将不需要记录在案的解决方案.而且我也知道UX准则强烈建议不要这样做.因此,将其写给我作为答案没有任何意义.

推荐答案

根据开发人员文档,在不向其显示任何相关通知的情况下显示图标不是一个好主意,

It is not good idea to show an icon without show him any related Notification, As per developer docs,

状态栏通知需要以下所有条件:

A status bar notification requires all of the following:

  • 您感兴趣的状态栏图标
  • 扩展视图的标题和扩展消息(除非您定义自定义扩展视图)
  • PendingIntent,当单击通知以显示相关信息时将被触发.

因此,如果您不想破坏用户体验,那么强烈建议您遵循UX准则

So if you don't want to ruin the User Experience then it's highly recommendable to follow UX guidelines,

从逻辑上讲:状态栏由两部分组成-用户(通知)和系统(其他).如果系统允许在系统部件中放置某些内容–将会有太多无用的图标(由其他应用程序发布),并且不会保持一致性,并且可能破坏所有使用体验.

logically: There are two parts of status bar – user (notifications) and system (other). If system allows to put something to system part – there will be so much useless icons (posted by other applications), and there will be no consistency, and it might break all of the use experience.

找到了一个答案,为您提供了此处访问默认状态栏警报"图标的方法.在手机上启动Root之后,您就可以做到了.YouTube上有一些技巧,关于同一主题的一些答案建议使用Notification来显示图标,但将Notification Text留空,以便用户只能看到图标,方式.

Found an answer which provide you way to access default status bar icon of Alarm it is here. After Root your mobile you can do it.There are some tricks on youtube doing it, Some answers on same topic suggest to show icon using Notification but put Notification Text empty so that user will only see icon, but again it's not a way.

这里.

This is code for Status bar implementation in System where Left and Right Icons(i.e System Icons) are described may you can get some lead from there. Answer that describe it is here.

这篇关于在状态栏中仅显示一个小图标,而不显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆