我想在系统的状态栏中一直显示一个小图标,而不在系统的通知托盘中显示通知本身。
我尝试使用自定义布局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());
我很清楚,为了实现我的目标,我需要非正式的解决方案。而且我也知道UX指南强烈建议不要这样做。所以你写这个作为答案是没有意义的。