More than one status bar notification
Can I create more than one notification in st开发者_运维技巧atus bar from one program(Service) or I should create new Activity with a clickable list(for example LinearLayout
) of objects?
You certainly can create more than one notification from a service or application, but you have to ask yourself is, as a user, you would want an application to spam notifications to you. I've been using one notification in my remote service and reusing the same notification by just updating its content. Here is an example:
public void onPlaybackStarted(int currentTrack, Show show) {
notificationManager.cancel(R.layout.notification_playing);
notification.tickerText = show.getTracks().get(currentTrack).getName();
if (notificationView == null) {
notificationView = new RemoteViews(getPackageName(), R.layout.notification_playing);
}
notificationView.setTextViewText(R.id.notification_playing_track, show.getTracks().get(currentTrack).getName());
notificationView.setTextViewText(R.id.notification_playing_band, show.getArtist());
notificationView.setTextViewText(R.id.notification_playing_date, show.getDate());
Intent intent = new Intent(TrackPlayerService.this, ListTracksActivity.class)
.putExtra("track", currentTrack)
.putExtra("artist", show.getArtist())
.putExtra("date", show.getDate())
.putExtra("location", show.getLocation())
.putExtra("venue", show.getVenue())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.contentView = notificationView;
notification.contentIntent = PendingIntent.getActivity(TrackPlayerService.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(R.layout.notification_playing, notification);
}
If your notifications are not revolving, meaning you need to notify the user about 3 or 4 different things simultaneously, then having a notification that opens a ListActivity would be the best way to go.
精彩评论