我能否从一个程序(服务)在状态栏中创建多个通知,或者我应该创建具有可点击对象列表(例如 LinearLayout
)的新 Activity ?
最佳答案
您当然可以从一项服务或应用程序创建多个通知,但您必须问问自己,作为用户,您是否希望应用程序向您发送垃圾邮件通知。我一直在我的远程服务中使用一个通知,并通过仅更新其内容来重复使用相同的通知。这是一个例子:
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);
}
如果您的通知不是循环的,这意味着您需要同时通知用户 3 或 4 件不同的事情,那么打开 ListActivity 的通知将是最好的方式。
关于android - 多个状态栏通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5435581/