setContent不适用于NotificationCompat.Builder

时间:2013-10-18 11:48:55

标签: android service notifications

我想将自定义布局显示为服务中的通知。

这是我在Service类中的方法:

private void fireNotification() {
    Log.d("Clock", "Fire!");
    if (this.remoteViews == null) {
        Log.d("Clock", "!=null");

        remoteViews = new RemoteViews(getPackageName(),
                R.layout.custom_notification);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher).setContent(
                remoteViews);

        // Creates an explicit intent for an Activity in your app
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        mBuilder.setContentIntent(contentIntent);

        startForeground(1, mBuilder.build());
    }
}

custom_notification.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="3dp"
          android:background="@android:color/white"
          >

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#000" />

<ImageView android:id="@+id/image"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_marginRight="10dp"
          android:background="@android:drawable/ic_menu_help"
          />
</LinearLayout>

但默认通知显示我的图标! 什么是哇?

编辑: 我在模拟器上运行我的应用程序,它在Android 4上工作但在我的手机中使用姜饼却无法正常工作!

2 个答案:

答案 0 :(得分:0)

您必须将所需图像设置为图像视图custom_layout中的内容

contentView.setImageViewResource(R.id.image, R.drawable.icon);

答案 1 :(得分:0)

根据Google's bug list page,NotificationCompat.Builder不支持Android 2.x上的setContent。

错误页面(#6)的可能解决方法:

notification = builder.build(); notification.contentView = contentView;