【发布时间】:2016-09-18 03:11:02
【问题描述】:
我想在status bar 中不断显示小图标,而不在系统的notification 托盘上显示通知本身。
我尝试使用自定义layout 并将根元素的可见性设置为View.GONE 或View.INVISIBLE,但对于两者,系统都会忽略它。
我也尝试设置根元素的高度,但系统也忽略了这一点。
这是我的代码:
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());
我很清楚,为了实现我的目标,我不需要记录解决方案。而且我也知道用户体验指南强烈建议不要做这样的事情。所以没有必要给我写这个作为答案。
【问题讨论】:
-
您想在应用启动时设置这个小图标并保持不变?
-
Afaik 你在寻找什么它不可能在通知区域中没有永久通知。这只有在定制的安卓系统上才有可能,所以对你没有用处
-
@mwisnicki 是的,完全正确。它已经很好用了,我只是在寻找一种方法来摆脱托盘中的通知并只显示小图标。
-
@Tazz 我很清楚这不是一件容易解决的事情,因为我花了半年多的时间寻找绕过这个限制的方法。
-
@yshahak 您不能为此使用通知,它们不是这样设计的,唯一想到的解决方案是在屏幕上绘制一个图标并将其放在通知托盘区域并计算如何您通过 NotificationListener 收到的许多通知(您需要用户的权限)因此您将图标向右移动,否则它将与该图标重叠。您还需要用户的权限才能在屏幕上绘制。在这里查看如何在其他应用上绘图:stackoverflow.com/questions/20461979/…
标签: android android-layout notifications