18

を使用しNotificationCompat.BuilderてAndroidバージョンで通知を表示し、通知にカスタムレイアウトを使用します。
カスタムレイアウトはAndroid3以降(APIレベル11)で正常に機能しますが、APIレベル10以下では表示されません。エミュレータで2.3と2.2でテストしました。

これが私のコードです:

    Builder builder = new NotificationCompat.Builder(getApplicationContext());

    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
    contentView.setImageViewResource(R.id.notImage, R.drawable.stat_icon);
    contentView.setTextViewText(R.id.notTitle, getResources().getString(R.string.streamPlaying));
    contentView.setTextViewText(R.id.notText, StartActivity.streamName + " " + getResources().getString(R.string.playing));

    builder
            .setContentTitle(getResources().getString(R.string.streamPlaying))
            .setContentText(StartActivity.streamName + " " + getResources().getString(R.string.playing))
            .setSmallIcon(R.drawable.stat_icon)
            .setContentIntent(pendingIntent)
            .setOngoing(true)
            .setWhen(0)
            .setTicker(StartActivity.streamName + " " + getResources().getString(R.string.playing))
            .setContent(contentView);

    not = builder.build();

本当に基本的です。レイアウトファイルは正しく、android.comの通知チュートリアルと同じで、間違いがないことを確認しています。;)
覚えておいてください:3.0以降では正常に動作しますが、2.3以下では動作しません。

4

1 に答える 1

38

これはサポートライブラリのバグである可能性があります-この問題を参照してください。

contentViewを直接適用して回避する必要がある場合があります。

not.contentView = contentView;
于 2012-09-24T23:55:45.073 に答える