I am using MediaPlayer
in my app. I am adding an Ongoing Notification
with media player controls (such as previous, next and stop buttons) in my app, so that users don't have to go in the app to access those controls. It looks fine on platforms 4.x. Here is the screenshot of the notification on Android 4.2.2.
But, on Android 2.2, it looks like this:
My code is as follows:
private Notification generateNotification() {
Log.d(TAG, "generateNotification called");
Intent actionIntent = new Intent(getApplicationContext(),
AartiActivity.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews mNotificationView = new RemoteViews(getPackageName(),
R.layout.notification_view);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext());
builder.setSmallIcon(R.drawable.icon);
builder.setContent(mNotificationView);
builder.setOngoing(true);
builder.setTicker("Aarti Sangrah");
builder.setContentIntent(pi);
mNotificationView.setImageViewResource(R.id.imgAppIc, R.drawable.icon);
mNotificationView.setTextViewText(R.id.txtAartiPlaying, mediaName);
return builder.build();
}// generateNotification
And then, I called startForeground(1, generateNotification());
in onPrepared().
The Remote Views is available since API level 1. And, it is well supported also. I read somewhere, it was not supported prior to Honeycomb. But, on several devices having Android 2.x, this feature is available. Also, in order to check this, I looked at the source code of Music Player of Android 2.2 from here.
Here, is the snippet from its Music Player Service.
RemoteViews views = new RemoteViews(getPackageName(),
R.layout.statusbar);
views.setOnClickPendingIntent(R.id.btnTest, PendingIntent
.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(),
MusicBrowserActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT));
views.setImageViewResource(R.id.icon,
R.drawable.stat_notify_musicplayer);
if (getAudioId() < 0) {
// streaming
views.setTextViewText(R.id.trackname, getPath());
views.setTextViewText(R.id.artistalbum, null);
} else {
String artist = getArtistName();
views.setTextViewText(R.id.trackname, getTrackName());
if (artist == null || artist.equals(MediaStore.UNKNOWN_STRING)) {
artist = getString(R.string.unknown_artist_name);
}
String album = getAlbumName();
if (album == null || album.equals(MediaStore.UNKNOWN_STRING)) {
album = getString(R.string.unknown_album_name);
}
views.setTextViewText(
R.id.artistalbum,
getString(R.string.notification_artist_album, artist,
album));
}
Notification status = new Notification();
status.contentView = views;
status.flags |= Notification.FLAG_ONGOING_EVENT;
status.icon = R.drawable.stat_notify_musicplayer;
status.contentIntent = PendingIntent.getActivity(this, 0,
new Intent("com.android.music.PLAYBACK_VIEWER")
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0);
startForeground(PLAYBACKSERVICE_STATUS, status);
The Remote Views has been used in this code. It had two TextViews
. I modified the code by adding a button to it, and also performed action on button click. Everything worked fine on every platform.
Same thing, I want with my application. But, on 2.2, it looks as I had shown above in the screenshot. I thought it is because of white colors text and button so tried changing the colors of button and text, but no luck. As far as I understand, only thing I figured out is remote view is not inflated on Android 2.2 (in my case). I am not getting why notification is not appearing properly on Android 2.x platforms.
I solved my problem. Here is my solution.
private Notification generateNotification() {
Log.d(TAG, "generateNotification called");
Intent actionIntent = new Intent(getApplicationContext(),
AartiActivity.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews mNotificationView = new RemoteViews(getPackageName(),
R.layout.notification_view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext());
builder.setSmallIcon(R.drawable.icon);
builder.setContent(mNotificationView);
builder.setOngoing(true);
builder.setTicker("Aarti Sangrah");
builder.setContentIntent(pi);
mNotificationView.setImageViewResource(R.id.imgAppIc,
R.drawable.icon);
mNotificationView.setTextViewText(R.id.txtAartiPlaying, mediaName);
mNotificationView.setTextColor(R.id.txtAartiPlaying, getResources()
.getColor(android.R.color.holo_orange_light));
return builder.build();
} else {
mNotificationView.setTextViewText(R.id.txtAartiPlaying, mediaName);
mNotificationView.setTextColor(R.id.txtAartiPlaying, getResources()
.getColor(android.R.color.holo_orange_light));
Notification statusNotification = new Notification();
statusNotification.contentView = mNotificationView;
statusNotification.flags |= Notification.FLAG_ONGOING_EVENT;
statusNotification.icon = R.drawable.icon;
statusNotification.contentIntent = PendingIntent.getActivity(this,
0, new Intent(getApplicationContext(), AartiActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0);
return statusNotification;
}
}// generateNotification
But, I am quiet surprised. NotificationCompat.Builder is available in support package and it has been provided for backward compatibility. But, in my case it is working only on Honeycomb and above. As per this section:
Not all notification features are available for a particular version, even though the methods to set them are in the support library class NotificationCompat.Builder. For example, action buttons, which depend on expanded notifications, only appear on Android 4.1 and higher, because expanded notifications themselves are only available on Android 4.1 and higher.
But, I don't think that, this is applicable for my case. NotificationCompat.Builder works with remote views but didn't worked on 2.x(at least in my case, may be I am wrong or missing something). If anybody has any information or resources in this regard, please share it so that I can figure out my mistake.