1

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

这是我在 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 上运行,但在我的手机中使用姜饼,它无法正常工作!

4

2 回答 2

0

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

contentView.setImageViewResource(R.id.image, R.drawable.icon);
于 2013-10-18T12:14:09.950 回答
0

根据Google 的错误列表页面,NotificationCompat.Builder 不支持 Android 2.x 上的 setContent。

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

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

于 2015-03-06T15:16:26.713 回答