Android自定义通知无法显示

Spe*_*ort 2 android android-notifications

我无法收到要显示的自定义布局的通知.如果我使用以下(旧)代码:

notification = new Notification();
notification.icon = R.drawable.icon;
notification.tickerText = currentTickerText;
notification.when = System.currentTimeMillis();
notification.setLatestEventInfo(context, (CharSequence)currentTitle, (CharSequence)"", pendingIntent);
Run Code Online (Sandbox Code Playgroud)

一切都按预期工作,通知出现.但是,如果我使用以下(新)代码:

contentView = new RemoteViews(context.getPackageName(), R.layout.service_notification);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.title, (CharSequence)currentTitle);
contentView.setTextViewText(R.id.text, (CharSequence)"");
contentView.setViewVisibility(R.id.pgStatus, View.GONE);

notification = new Notification();
notification.tickerText = currentTickerText;
notification.when = System.currentTimeMillis();

notification.contentView = contentView;
notification.contentIntent = pendingIntent;
Run Code Online (Sandbox Code Playgroud)

通知永远不会显示.我没有给出错误,所以我不确定在哪里寻找问题.这是service_notification布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" >
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:text="This is the title."
        style="@style/NotificationTitle" />
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
        android:text="This is the status"
        style="@style/NotificationText" />
    <ProgressBar
        android:id="@+id/pgStatus"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_toRightOf="@+id/image" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

此外,我在Android 2.2模拟器上运行它,如果这有所作为.

任何人都有任何可能出错的想法或我如何追查这个问题?我有点不知所措,因为我没有错误消息可以解决.谢谢!

小智 11

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

  • 状态栏的图标
  • 标题和消息,除非您定义自定义通知布局
  • PendingIntent,在选择通知时触发

状态栏通知 - 创建通知

您没有指定图标,这意味着您的通知无效.

(但是,在框架方面,在这种情况下没有正确的错误消息/异常是不好的)


编辑:刚试过这个,我可以确认Android 2.2评论中的"隐形"通知.它适用于2.3.3.,似乎你偶然发现了一个已经修复过的bug.

有问题的一行对我来说如下:

contentView.setViewVisibility(R.id.pgStatus, View.GONE);
Run Code Online (Sandbox Code Playgroud)

如果我发表评论,一切正常.当我android:visibility="gone"在XML布局中添加ProgressBar 时它也有效,它具有基本相同的效果.但是一旦我打电话setViewVisibility()给酒吧,一切都会分崩离析.

所以我建议从bugtracker评论中解决这个问题,将ProgressBar包装成LinearLayout并改变它的可见性.